
##################################################
#############Table A3: Summary Statistics##########
##################################################

summaryStatsData<-dips73[c("victim", "anyrepression", "elected", "quotient_diff", "UP",
                           "surname", "incumbent_orpastelected",
                           "dynastic", "gender", "CD_presscoverage", "OnList")]
summaryStatsTable<-stargazer(summaryStatsData, header=FALSE, 
                             covariate.labels=c("Physical coercion", "Overall repression", "Election",
                                                "Quotient difference", "Popular Unity (UP)", "Surname",
                                                "Incumbent", "Dynastic", "Gender", "Press attention",
                                                "Targeted"),
                             title="Summary Statistics", label = "table:repressedpols_summarystats")

summaryStatsTableFinal<-c(format(summaryStatsTable))
##write(summaryStatsTableFinal, "summaryStatsTableFinal.tex")


######################################################################
#############Table A4: Summary Statistics, estimating sample##########
######################################################################

summary(RDestimate(victim~quotient_diff, data=dips73, cluster=dips73$Distrito))

summaryStats_es<-subset(summaryStatsData, abs(quotient_diff)<2.807)

summaryStatsTable_es<-stargazer(summaryStats_es, header=FALSE, 
                                covariate.labels=c("Physical coercion", "Overall repression", "Election",
                                                   "Quotient difference", "Popular Unity (UP)", "Surname",
                                                   "Incumbent", "Dynastic", "Gender", "Press attention",
                                                   "Targeted"),
                                title="Summary Statistics (RD Main Estimating Sample)", label = "table:repressedpols_summarystats_es")

summaryStatsTableFinal_es<-c(format(summaryStatsTable_es))
##write(summaryStatsTableFinal_es, "summaryStatsTableFinal_es.tex")


######################################################################
##############Table A5: Vote share and targeting#####################
######################################################################

#Targeting, model 1 -- no controls
targetlogit_noc<-logitCoefficients(dips73, c('search', 'vote_perc'), iterations)
functional_iterations=length(unlist(targetlogit_noc))/2

elected_target_results=as.data.frame(t(matrix(unlist(targetlogit_noc), ncol=functional_iterations)))
elected_target_reg<-glm(search~vote_perc, data=dips73, family="binomial")
elected_elected_target<-format(round(summary(elected_target_reg)$coefficients[2,1], 2))
##write(elected_elected_target, "elected_elected_target.tex")
elected_target_AIC<-format(round(summary(elected_target_reg)$aic, 2))
##write(elected_target_AIC, "elected_target_AIC.tex")

elected_target_lower<-format(round(quantile(elected_target_results$V2,probs=.05), 2))
##write(elected_target_lower, "elected_target_lower.tex")
elected_target_upper<-format(round(quantile(elected_target_results$V2,probs=.95), 2))
##write(elected_target_upper, "elected_target_upper.tex")

elected_constant_target<-format(round(summary(elected_target_reg)$coefficients[1,1], 2))
##write(elected_constant_target, "elected_constant_target.tex")
elected_constant_target_lower<-format(round(quantile(elected_target_results$V1,probs=.05), 2))
##write(elected_constant_target_lower, "elected_constant_target_lower.tex")
elected_constant_target_upper<-format(round(quantile(elected_target_results$V1,probs=.95), 2))
##write(elected_constant_target_upper, "elected_constant_target_upper.tex")

##Targeting, model 2 --  controls
targetlogit_c<-logitCoefficients(dips73, c('search', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(targetlogit_c))/2

electedc_target_results=as.data.frame(t(matrix(unlist(targetlogit_c), ncol=functional_iterations)))

electedc_target_reg<-glm(search~vote_perc+surname+incumbent_orpastelected+dynastic, data=dips73, family="binomial")
electedc_elected_target<-format(round(summary(electedc_target_reg)$coefficients[2,1], 2))
##write(electedc_elected_target, "electedc_elected_target.tex")
electedc_target_AIC<-format(round(summary(electedc_target_reg)$aic, 2))
##write(electedc_target_AIC, "electedc_target_AIC.tex")

electedc_target_lower<-format(round(quantile(electedc_target_results$V2,probs=.05), 2))
##write(electedc_target_lower, "electedc_target_lower.tex")
electedc_target_upper<-format(round(quantile(electedc_target_results$V2,probs=.95), 2))
##write(electedc_target_upper, "electedc_target_upper.tex")

electedc_constant_target<-format(round(summary(electedc_target_reg)$coefficients[1,1], 2))
##write(electedc_constant_target, "electedc_constant_target.tex")
electedc_constant_target_lower<-format(round(quantile(electedc_target_results$V1,probs=.05), 2))
##write(electedc_constant_target_lower, "electedc_constant_target_lower.tex")
electedc_constant_target_upper<-format(round(quantile(electedc_target_results$V1,probs=.95), 2))
##write(electedc_constant_target_upper, "electedc_constant_target_upper.tex")

##Targeting, model 3 -- full controls
target_logit_pdc<-logitCoefficients(dips73, c('search', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(target_logit_pdc))/2

electedpdc_target_results=as.data.frame(t(matrix(unlist(target_logit_pdc), ncol=functional_iterations)))
electedpdc_target_reg<-glm(search~vote_perc+factor(list)+factor(agrupacion)+surname+dynastic+incumbent_orpastelected, data=dips73, family="binomial")

electedpdc_elected_target<-format(round(summary(electedpdc_target_reg)$coefficients[2,1], 2))
##write(electedpdc_elected_target, "electedpdc_elected_target.tex")
electedpdc_target_AIC<-format(round(summary(electedpdc_target_reg)$aic, 2))
##write(electedpdc_target_AIC, "electedpdc_target_AIC.tex")

electedpdc_target_lower<-format(round(quantile(electedpdc_target_results$V2,probs=.05), 2))
##write(electedpdc_target_lower, "electedpdc_target_lower.tex")
electedpdc_target_upper<-format(round(quantile(electedpdc_target_results$V2,probs=.95), 2))
##write(electedpdc_target_upper, "electedpdc_target_upper.tex")

electedpdc_constant_target<-format(round(summary(electedpdc_target_reg)$coefficients[1,1], 2))
##write(electedpdc_constant_target, "electedpdc_constant_target.tex")
electedpdc_constant_target_lower<-format(round(quantile(electedpdc_target_results$V1,probs=.05), 2))
##write(electedpdc_constant_target_lower, "electedpdc_constant_target_lower.tex")
electedpdc_constant_target_upper<-format(round(quantile(electedpdc_target_results$V1,probs=.95), 2))
##write(electedpdc_constant_target_upper, "electedpdc_constant_target_upper.tex")



######################################################################
################Table B1: T-test table results########################
######################################################################

##write(format(round(as.numeric(as.character(ttest_anyrepression$elected[1])), 2)), 'mean_anyrepression_elected.tex')
##write(format(round(as.numeric(as.character(ttest_anyrepression$unelected[1])), 2)), 'mean_anyrepression_unelected.tex')
##write(format(round(as.numeric(as.character(ttest_anyrepression$difference[1])), 2)), 'mean_difference_anyrepression.tex')

##write(format(round(as.numeric(as.character(ttest_physicalcoercion$elected[1])), 2)), 'mean_physicalcoercion_elected.tex')
##write(format(round(as.numeric(as.character(ttest_physicalcoercion$unelected[1])), 2)), 'mean_physicalcoercion_unelected.tex')
##write(format(round(as.numeric(as.character(ttest_physicalcoercion$difference[1])), 2)), 'mean_physicalcoercion_difference.tex')

##write(format(round(as.numeric(as.character(ttest_anyrepression_nousopo$elected[1])), 2)), 'mean_anyrepression_elected_nousopo.tex')
##write(format(round(as.numeric(as.character(ttest_anyrepression_nousopo$unelected[1])), 2)), 'mean_anyrepression_unelected_nousopo.tex')
##write(format(round(as.numeric(as.character(ttest_anyrepression_nousopo$difference[1])), 2)), 'mean_difference_anyrepression_nousopo.tex')

##write(format(round(as.numeric(as.character(ttest_victim_nousopo$elected[1])), 2)), 'mean_physicalcoercion_elected_nousopo.tex')
##write(format(round(as.numeric(as.character(ttest_victim_nousopo$unelected[1])), 2)), 'mean_physicalcoercion_unelected_nousopo.tex')
##write(format(round(as.numeric(as.character(ttest_victim_nousopo$difference[1])), 2)), 'mean_difference_physicalcoercion_nousopo.tex')


##write(format(round(as.numeric(as.character(ttest_anyrepression_leftDC$elected[1])), 2)), 'mean_anyrepression_elected_leftDC.tex')
##write(format(round(as.numeric(as.character(ttest_anyrepression_leftDC$unelected[1])), 2)), 'mean_anyrepression_unelected_leftDC.tex')
##write(format(round(as.numeric(as.character(ttest_anyrepression_leftDC$difference[1])), 2)), 'mean_difference_anyrepression_leftDC.tex')

##write(format(round(as.numeric(as.character(ttest_victim_leftDC$elected[1])), 2)), 'mean_physicalcoercion_elected_leftDC.tex')
##write(format(round(as.numeric(as.character(ttest_victim_leftDC$unelected[1])), 2)), 'mean_physicalcoercion_unelected_leftDC.tex')
##write(format(round(as.numeric(as.character(ttest_victim_leftDC$difference[1])), 2)), 'mean_difference_physicalcoercion_leftDC.tex')

######################################################################
######################Table B2: Exile table###########################
######################################################################

ttest_exiled<-ttest_electedvunelected(dips73$EXILE_LIST, dips73)
##write(format(round(as.numeric(as.character(ttest_exiled$elected[1])), 2)), 'mean_exiled_elected.tex')
##write(format(round(as.numeric(as.character(ttest_exiled$unelected[1])), 2)), 'mean_exiled_unelected.tex')
##write(format(round(as.numeric(as.character(ttest_exiled$difference[1])), 2)), 'mean_difference_exiled.tex')

ttest_exiled_nousopo<-ttest_electedvunelected(dips73_nousopo$EXILE_LIST, dips73_nousopo)
##write(format(round(as.numeric(as.character(ttest_exiled_nousopo$elected[1])), 2)), 'mean_exiled_nousopo_elected.tex')
##write(format(round(as.numeric(as.character(ttest_exiled_nousopo$unelected[1])), 2)), 'mean_exiled_nousopo_unelected.tex')
##write(format(round(as.numeric(as.character(ttest_exiled_nousopo$difference[1])), 2)), 'mean_difference_exiled_nousopo.tex')

ttest_exiled_leftDC<-ttest_electedvunelected(dips73_leftDC$EXILE_LIST, dips73_leftDC)
##write(format(round(as.numeric(as.character(ttest_exiled_leftDC$elected[1])), 2)), 'mean_exiled_leftDC_elected.tex')
##write(format(round(as.numeric(as.character(ttest_exiled_leftDC$unelected[1])), 2)), 'mean_exiled_leftDC_unelected.tex')
##write(format(round(as.numeric(as.character(ttest_exiled_leftDC$difference[1])), 2)), 'mean_difference_exiled_leftDC.tex')

######################################################################
###Figure B1: scatterplot vote share and overall repression##########
######################################################################

anyrepression_scatterplot<-ggplot(subset(dips73, vote_perc<40), aes(x=vote_perc, y=anyrepression))+geom_point()+
  geom_smooth(method='lm', formula=y~x, color='black')+theme_bw()+xlab('Vote Share')+ylab('Overall Repression')

#ggsave('anyrepression_scatterplot.pdf', anyrepression_scatterplot, height=3.5, width=5)


###############################################################################
#######Statistics generated for Tables B3 and B4 in main paper file############
###############################################################################


######################################################################
########Table B5: Overall repression and election#####################
######################################################################

#Overall repression, model 1 -- no controls
ar_e_logit_noc<-logitCoefficients(dips73, c('anyrepression', 'elected'), iterations)
functional_iterations=length(unlist(ar_e_logit_noc))/2

elected_anyrepression_e_results=as.data.frame(t(matrix(unlist(ar_e_logit_noc), ncol=functional_iterations)))
elected_anyrepression_e_reg<-glm(anyrepression~elected, data=dips73, family="binomial")
elected_elected_anyrepression_e<-format(round(summary(elected_anyrepression_e_reg)$coefficients[2,1], 2))
##write(elected_elected_anyrepression_e, "elected_elected_anyrepression_e.tex")
elected_anyrepression_e_AIC<-format(round(summary(elected_anyrepression_e_reg)$aic, 2))
##write(elected_anyrepression_e_AIC, "elected_anyrepression_e_AIC.tex")

elected_anyrepression_e_lower<-format(round(quantile(elected_anyrepression_e_results$V2,probs=.05), 2))
##write(elected_anyrepression_e_lower, "elected_anyrepression_e_lower.tex")
elected_anyrepression_e_upper<-format(round(quantile(elected_anyrepression_e_results$V2,probs=.95), 2))
##write(elected_anyrepression_e_upper, "elected_anyrepression_e_upper.tex")

elected_constant_anyrepression_e<-format(round(summary(elected_anyrepression_e_reg)$coefficients[1,1], 2))
##write(elected_constant_anyrepression_e, "elected_constant_anyrepression_e.tex")
elected_constant_anyrepression_e_lower<-format(round(quantile(elected_anyrepression_e_results$V1,probs=.05), 2))
##write(elected_constant_anyrepression_e_lower, "elected_constant_anyrepression_e_lower.tex")
elected_constant_anyrepression_e_upper<-format(round(quantile(elected_anyrepression_e_results$V1,probs=.95), 2))
##write(elected_constant_anyrepression_e_upper, "elected_constant_anyrepression_e_upper.tex")

##Overall repression, model 2 --  controls
ar_e_logit_c<-logitCoefficients(dips73, c('anyrepression', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(ar_e_logit_c))/2

electedc_anyrepression_e_results=as.data.frame(t(matrix(unlist(ar_e_logit_c), ncol=functional_iterations)))

electedc_anyrepression_e_reg<-glm(anyrepression~elected+surname+incumbent_orpastelected+dynastic, data=dips73, family="binomial")
electedc_elected_anyrepression_e<-format(round(summary(electedc_anyrepression_e_reg)$coefficients[2,1], 2))
##write(electedc_elected_anyrepression_e, "electedc_elected_anyrepression_e.tex")
electedc_anyrepression_e_AIC<-format(round(summary(electedc_anyrepression_e_reg)$aic, 2))
##write(electedc_anyrepression_e_AIC, "electedc_anyrepression_e_AIC.tex")

electedc_anyrepression_e_lower<-format(round(quantile(electedc_anyrepression_e_results$V2,probs=.05), 2))
##write(electedc_anyrepression_e_lower, "electedc_anyrepression_e_lower.tex")
electedc_anyrepression_e_upper<-format(round(quantile(electedc_anyrepression_e_results$V2,probs=.95), 2))
##write(electedc_anyrepression_e_upper, "electedc_anyrepression_e_upper.tex")

electedc_constant_anyrepression_e<-format(round(summary(electedc_anyrepression_e_reg)$coefficients[1,1], 2))
##write(electedc_constant_anyrepression_e, "electedc_constant_anyrepression_e.tex")
electedc_constant_anyrepression_e_lower<-format(round(quantile(electedc_anyrepression_e_results$V1,probs=.05), 2))
##write(electedc_constant_anyrepression_e_lower, "electedc_constant_anyrepression_e_lower.tex")
electedc_constant_anyrepression_e_upper<-format(round(quantile(electedc_anyrepression_e_results$V1,probs=.95), 2))
##write(electedc_constant_anyrepression_e_upper, "electedc_constant_anyrepression_e_upper.tex")

##Overall repression, model 3 -- full controls
ar_e_logit_pdc<-logitCoefficients(dips73, c('anyrepression', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(ar_e_logit_pdc))/2

electedpdc_anyrepression_e_results=as.data.frame(t(matrix(unlist(ar_e_logit_pdc), ncol=functional_iterations)))
electedpdc_anyrepression_e_reg<-glm(anyrepression~elected+factor(list)+factor(agrupacion)+surname+dynastic+incumbent_orpastelected, data=dips73, family="binomial")

electedpdc_elected_anyrepression_e<-format(round(summary(electedpdc_anyrepression_e_reg)$coefficients[2,1], 2))
##write(electedpdc_elected_anyrepression_e, "electedpdc_elected_anyrepression_e.tex")
electedpdc_anyrepression_e_AIC<-format(round(summary(electedpdc_anyrepression_e_reg)$aic, 2))
##write(electedpdc_anyrepression_e_AIC, "electedpdc_anyrepression_e_AIC.tex")

electedpdc_anyrepression_e_lower<-format(round(quantile(electedpdc_anyrepression_e_results$V2,probs=.05), 2))
##write(electedpdc_anyrepression_e_lower, "electedpdc_anyrepression_e_lower.tex")
electedpdc_anyrepression_e_upper<-format(round(quantile(electedpdc_anyrepression_e_results$V2,probs=.95), 2))
##write(electedpdc_anyrepression_e_upper, "electedpdc_anyrepression_e_upper.tex")

electedpdc_constant_anyrepression_e<-format(round(summary(electedpdc_anyrepression_e_reg)$coefficients[1,1], 2))
##write(electedpdc_constant_anyrepression_e, "electedpdc_constant_anyrepression_e.tex")
electedpdc_constant_anyrepression_e_lower<-format(round(quantile(electedpdc_anyrepression_e_results$V1,probs=.05), 2))
##write(electedpdc_constant_anyrepression_e_lower, "electedpdc_constant_anyrepression_e_lower.tex")
electedpdc_constant_anyrepression_e_upper<-format(round(quantile(electedpdc_anyrepression_e_results$V1,probs=.95), 2))
##write(electedpdc_constant_anyrepression_e_upper, "electedpdc_constant_anyrepression_e_upper.tex")


###Logistic regression, excluding USPOO

##Any repression, model 1 -- no controls
ar_e_logit_noc_nousopo<-logitCoefficients(dips73_nousopo, c('anyrepression', 'elected'), iterations)
functional_iterations=length(unlist(ar_e_logit_noc_nousopo))/2

elected_anyrepression_e_logit_results_nousopo=as.data.frame(t(matrix(unlist(ar_e_logit_noc_nousopo), ncol=functional_iterations)))
elected_anyrepression_e_nousopo_reg<-glm(anyrepression~elected, data=dips73_nousopo, family="binomial")
elected_elected_anyrepression_e_nousopo<-format(round(summary(elected_anyrepression_e_nousopo_reg)$coefficients[2,1], 2))
##write(elected_elected_anyrepression_e_nousopo, "elected_elected_anyrepression_e_nousopo.tex")
elected_anyrepression_e_nousopo_AIC<-format(round(summary(elected_anyrepression_e_nousopo_reg)$aic, 2))
##write(elected_anyrepression_e_nousopo_AIC, "elected_anyrepression_e_nousopo_AIC.tex")

elected_anyrepression_e_lower_nousopo<-format(round(quantile(elected_anyrepression_e_logit_results_nousopo$V2,probs=.05), 2))
##write(elected_anyrepression_e_lower_nousopo, "elected_anyrepression_e_lower_nousopo.tex")
elected_anyrepression_e_upper_nousopo<-format(round(quantile(elected_anyrepression_e_logit_results_nousopo$V2,probs=.95), 2))
##write(elected_anyrepression_e_upper_nousopo, "elected_anyrepression_e_upper_nousopo.tex")

elected_constant_anyrepression_e_nousopo<-format(round(summary(elected_anyrepression_e_nousopo_reg)$coefficients[1,1], 2))
##write(elected_constant_anyrepression_e_nousopo, "elected_constant_anyrepression_e_nousopo.tex")
elected_constant_anyrepression_e_lower_nousopo<-format(round(quantile(elected_anyrepression_e_logit_results_nousopo$V1,probs=.05), 2))
##write(elected_constant_anyrepression_e_lower_nousopo, "elected_constant_anyrepression_e_lower_nousopo.tex")
elected_constant_anyrepression_e_upper_nousopo<-format(round(quantile(elected_anyrepression_e_logit_results_nousopo$V1,probs=.95), 2))
##write(elected_constant_anyrepression_e_upper_nousopo, "elected_constant_anyrepression_e_upper_nousopo.tex")

##Any repression, model 2 -- party controls
ar_e_logit_c_nousopo<-logitCoefficients(dips73_nousopo, c('anyrepression', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(ar_e_logit_c_nousopo))/2

electedc_anyrepression_e_logit_results_nousopo=as.data.frame(t(matrix(unlist(ar_e_logit_c_nousopo), ncol=functional_iterations)))

electedc_anyrepression_e_nousopo_reg<-glm(anyrepression~elected+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo, family="binomial")
electedc_elected_anyrepression_e_nousopo<-format(round(summary(electedc_anyrepression_e_nousopo_reg)$coefficients[2,1], 2))
##write(electedc_elected_anyrepression_e_nousopo, "electedc_elected_anyrepression_e_nousopo.tex")
electedc_anyrepression_e_nousopo_AIC<-format(round(summary(electedc_anyrepression_e_nousopo_reg)$aic, 2))
##write(electedc_anyrepression_e_nousopo_AIC, "electedc_anyrepression_e_nousopo_AIC.tex")

electedc_anyrepression_e_lower_nousopo<-format(round(quantile(electedc_anyrepression_e_logit_results_nousopo$V2,probs=.05), 2))
##write(electedc_anyrepression_e_lower_nousopo, "electedc_anyrepression_e_lower_nousopo.tex")
electedc_anyrepression_e_upper_nousopo<-format(round(quantile(electedc_anyrepression_e_logit_results_nousopo$V2,probs=.95), 2))
##write(electedc_anyrepression_e_upper_nousopo, "electedc_anyrepression_e_upper_nousopo.tex")

electedc_constant_anyrepression_e_nousopo<-format(round(summary(electedc_anyrepression_e_nousopo_reg)$coefficients[1,1], 2))
##write(electedc_constant_anyrepression_e_nousopo, "electedc_constant_anyrepression_e_nousopo.tex")
electedc_constant_anyrepression_e_lower_nousopo<-format(round(quantile(electedc_anyrepression_e_logit_results_nousopo$V1,probs=.05), 2))
##write(electedc_constant_anyrepression_e_lower_nousopo, "electedc_constant_anyrepression_e_lower_nousopo.tex")
electedc_constant_anyrepression_e_upper_nousopo<-format(round(quantile(electedc_anyrepression_e_logit_results_nousopo$V1,probs=.95), 2))
##write(electedc_constant_anyrepression_e_upper_nousopo, "electedc_constant_anyrepression_e_upper_nousopo.tex")

##Any repression -- all covariates
ar_e_logit_pdc_nousopo<-logitCoefficients(dips73_nousopo, c('anyrepression', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(ar_e_logit_pdc_nousopo))/2

electedpdc_anyrepression_e_logit_results_nousopo=as.data.frame(t(matrix(unlist(ar_e_logit_pdc_nousopo), ncol=functional_iterations)))

electedpdc_anyrepression_e_nousopo_reg<-glm(anyrepression~elected+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo, family="binomial")
electedpdc_elected_anyrepression_e_nousopo<-format(round(summary(electedpdc_anyrepression_e_nousopo_reg)$coefficients[2,1], 2))
##write(electedpdc_elected_anyrepression_e_nousopo, "electedpdc_elected_anyrepression_e_nousopo.tex")
electedpdc_anyrepression_e_nousopo_AIC<-format(round(summary(electedpdc_anyrepression_e_nousopo_reg)$aic, 2))
##write(electedpdc_anyrepression_e_nousopo_AIC, "electedpdc_anyrepression_e_nousopo_AIC.tex")

electedpdc_anyrepression_e_lower_nousopo<-format(round(quantile(electedpdc_anyrepression_e_logit_results_nousopo$V2,probs=.05), 2))
##write(electedpdc_anyrepression_e_lower_nousopo, "electedpdc_anyrepression_e_lower_nousopo.tex")
electedpdc_anyrepression_e_upper_nousopo<-format(round(quantile(electedpdc_anyrepression_e_logit_results_nousopo$V2,probs=.95), 2))
##write(electedpdc_anyrepression_e_upper_nousopo, "electedpdc_anyrepression_e_upper_nousopo.tex")

electedpdc_constant_anyrepression_e_nousopo<-format(round(summary(electedpdc_anyrepression_e_nousopo_reg)$coefficients[1,1], 2))
##write(electedpdc_constant_anyrepression_e_nousopo, "electedpdc_constant_anyrepression_e_nousopo.tex")
electedpdc_constant_anyrepression_e_lower_nousopo<-format(round(quantile(electedpdc_anyrepression_e_logit_results_nousopo$V1,probs=.05), 2))
##write(electedpdc_constant_anyrepression_e_lower_nousopo, "electedpdc_constant_anyrepression_e_lower_nousopo.tex")
electedpdc_constant_anyrepression_e_upper_nousopo<-format(round(quantile(electedpdc_anyrepression_e_logit_results_nousopo$V1,probs=.95), 2))
##write(electedpdc_constant_anyrepression_e_upper_nousopo, "electedpdc_constant_anyrepression_e_upper_nousopo.tex")



###Logistic regression, including only UP and PDC

##Any repression, model 1 -- no controls
ar_e_logit_noc_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo, c('anyrepression', 'elected'), iterations)
functional_iterations=length(unlist(ar_e_logit_noc_leftDC_nousopo))/2

elected_anyrepression_e_logit_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(ar_e_logit_noc_leftDC_nousopo), ncol=functional_iterations)))
elected_anyrepression_e_leftDC_nousopo_reg<-glm(anyrepression~elected, data=dips73_leftDC_nousopo, family="binomial")
elected_elected_anyrepression_e_leftDC_nousopo<-format(round(summary(elected_anyrepression_e_leftDC_nousopo_reg)$coefficients[2,1], 2))
##write(elected_elected_anyrepression_e_leftDC_nousopo, "elected_elected_anyrepression_e_leftDC_nousopo.tex")
elected_anyrepression_e_leftDC_nousopo_AIC<-format(round(summary(elected_anyrepression_e_leftDC_nousopo_reg)$aic, 2))
##write(elected_anyrepression_e_leftDC_nousopo_AIC, "elected_anyrepression_e_leftDC_nousopo_AIC.tex")

elected_anyrepression_e_lower_leftDC_nousopo<-format(round(quantile(elected_anyrepression_e_logit_results_leftDC_nousopo$V2,probs=.05), 2))
##write(elected_anyrepression_e_lower_leftDC_nousopo, "elected_anyrepression_e_lower_leftDC_nousopo.tex")
elected_anyrepression_e_upper_leftDC_nousopo<-format(round(quantile(elected_anyrepression_e_logit_results_leftDC_nousopo$V2,probs=.95), 2))
##write(elected_anyrepression_e_upper_leftDC_nousopo, "elected_anyrepression_e_upper_leftDC_nousopo.tex")

elected_constant_anyrepression_e_leftDC_nousopo<-format(round(summary(elected_anyrepression_e_leftDC_nousopo_reg)$coefficients[1,1], 2))
##write(elected_constant_anyrepression_e_leftDC_nousopo, "elected_constant_anyrepression_e_leftDC_nousopo.tex")
elected_constant_anyrepression_e_lower_leftDC_nousopo<-format(round(quantile(elected_anyrepression_e_logit_results_leftDC_nousopo$V1,probs=.05), 2))
##write(elected_constant_anyrepression_e_lower_leftDC_nousopo, "elected_constant_anyrepression_e_lower_leftDC_nousopo.tex")
elected_constant_anyrepression_e_upper_leftDC_nousopo<-format(round(quantile(elected_anyrepression_e_logit_results_leftDC_nousopo$V1,probs=.95), 2))
##write(elected_constant_anyrepression_e_upper_leftDC_nousopo, "elected_constant_anyrepression_e_upper_leftDC_nousopo.tex")

##Any repression, model 2 -- controls
ar_e_logit_c_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo, c('anyrepression', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(ar_e_logit_c_leftDC_nousopo))/2

electedc_anyrepression_e_logit_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(ar_e_logit_c_leftDC_nousopo), ncol=functional_iterations)))

electedc_anyrepression_e_leftDC_nousopo_reg<-glm(anyrepression~elected+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo, family="binomial")
electedc_elected_anyrepression_e_leftDC_nousopo<-format(round(summary(electedc_anyrepression_e_leftDC_nousopo_reg)$coefficients[2,1], 2))
##write(electedc_elected_anyrepression_e_leftDC_nousopo, "electedc_elected_anyrepression_e_leftDC_nousopo.tex")
electedc_anyrepression_e_leftDC_nousopo_AIC<-format(round(summary(electedc_anyrepression_e_leftDC_nousopo_reg)$aic, 2))
##write(electedc_anyrepression_e_leftDC_nousopo_AIC, "electedc_anyrepression_e_leftDC_nousopo_AIC.tex")

electedc_anyrepression_e_lower_leftDC_nousopo<-format(round(quantile(electedc_anyrepression_e_logit_results_leftDC_nousopo$V2,probs=.05), 2))
##write(electedc_anyrepression_e_lower_leftDC_nousopo, "electedc_anyrepression_e_lower_leftDC_nousopo.tex")
electedc_anyrepression_e_upper_leftDC_nousopo<-format(round(quantile(electedc_anyrepression_e_logit_results_leftDC_nousopo$V2,probs=.95), 2))
##write(electedc_anyrepression_e_upper_leftDC_nousopo, "electedc_anyrepression_e_upper_leftDC_nousopo.tex")

electedc_constant_anyrepression_e_leftDC_nousopo<-format(round(summary(electedc_anyrepression_e_leftDC_nousopo_reg)$coefficients[1,1], 2))
##write(electedc_constant_anyrepression_e_leftDC_nousopo, "electedc_constant_anyrepression_e_leftDC_nousopo.tex")
electedc_constant_anyrepression_e_lower_leftDC_nousopo<-format(round(quantile(electedc_anyrepression_e_logit_results_leftDC_nousopo$V1,probs=.05), 2))
##write(electedc_constant_anyrepression_e_lower_leftDC_nousopo, "electedc_constant_anyrepression_e_lower_leftDC_nousopo.tex")
electedc_constant_anyrepression_e_upper_leftDC_nousopo<-format(round(quantile(electedc_anyrepression_e_logit_results_leftDC_nousopo$V1,probs=.95), 2))
##write(electedc_constant_anyrepression_e_upper_leftDC_nousopo, "electedc_constant_anyrepression_e_upper_leftDC_nousopo.tex")


##Any repression, all covariates
ar_e_logit_pdc_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo, c('anyrepression', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(ar_e_logit_pdc_leftDC_nousopo))/2

electedpdc_anyrepression_e_logit_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(ar_e_logit_pdc_leftDC_nousopo), ncol=functional_iterations)))

electedpdc_anyrepression_e_leftDC_nousopo_reg<-glm(anyrepression~elected+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo, family="binomial")
electedpdc_elected_anyrepression_e_leftDC_nousopo<-format(round(summary(electedpdc_anyrepression_e_leftDC_nousopo_reg)$coefficients[2,1], 2))
##write(electedpdc_elected_anyrepression_e_leftDC_nousopo, "electedpdc_elected_anyrepression_e_leftDC_nousopo.tex")
electedpdc_anyrepression_e_leftDC_nousopo_AIC<-format(round(summary(electedpdc_anyrepression_e_leftDC_nousopo_reg)$aic, 2))
##write(electedpdc_anyrepression_e_leftDC_nousopo_AIC, "electedpdc_anyrepression_e_leftDC_nousopo_AIC.tex")

electedpdc_anyrepression_e_lower_leftDC_nousopo<-format(round(quantile(electedpdc_anyrepression_e_logit_results_leftDC_nousopo$V2,probs=.05), 2))
##write(electedpdc_anyrepression_e_lower_leftDC_nousopo, "electedpdc_anyrepression_e_lower_leftDC_nousopo.tex")
electedpdc_anyrepression_e_upper_leftDC_nousopo<-format(round(quantile(electedpdc_anyrepression_e_logit_results_leftDC_nousopo$V2,probs=.95), 2))
##write(electedpdc_anyrepression_e_upper_leftDC_nousopo, "electedpdc_anyrepression_e_upper_leftDC_nousopo.tex")

electedpdc_constant_anyrepression_e_leftDC_nousopo<-format(round(summary(electedpdc_anyrepression_e_leftDC_nousopo_reg)$coefficients[1,1], 2))
##write(electedpdc_constant_anyrepression_e_leftDC_nousopo, "electedpdc_constant_anyrepression_e_leftDC_nousopo.tex")
electedpdc_constant_anyrepression_e_lower_leftDC_nousopo<-format(round(quantile(electedpdc_anyrepression_e_logit_results_leftDC_nousopo$V1,probs=.05), 2))
##write(electedpdc_constant_anyrepression_e_lower_leftDC_nousopo, "electedpdc_constant_anyrepression_e_lower_leftDC_nousopo.tex")
electedpdc_constant_anyrepression_e_upper_leftDC_nousopo<-format(round(quantile(electedpdc_anyrepression_e_logit_results_leftDC_nousopo$V1,probs=.95), 2))
##write(electedpdc_constant_anyrepression_e_upper_leftDC_nousopo, "electedpdc_constant_anyrepression_e_upper_leftDC_nousopo.tex")



###################################################################################
###Using log total votes as measure of popularity/mobilizing capacity (Table B6)###
###################################################################################

dips73$log_votes=log(dips73$Votos)
dips73_nousopo$log_votes=log(dips73_nousopo$Votos)
dips73_leftDC_nousopo$log_votes=log(dips73_leftDC_nousopo$Votos)

#Overall repression, model 1 -- no controls
ar_v_logit_noc<-logitCoefficients(dips73, c('anyrepression', 'log_votes'), iterations)
functional_iterations=length(unlist(ar_v_logit_noc))/2

elected_anyrepression_v_results=as.data.frame(t(matrix(unlist(ar_v_logit_noc), ncol=functional_iterations)))
elected_anyrepression_v_reg<-glm(anyrepression~log_votes, data=dips73, family="binomial")
elected_elected_anyrepression_e<-format(round(summary(elected_anyrepression_v_reg)$coefficients[2,1], 2))
##write(elected_elected_anyrepression_e, "elected_elected_anyrepression_v.tex")
elected_anyrepression_v_AIC<-format(round(summary(elected_anyrepression_v_reg)$aic, 2))
##write(elected_anyrepression_v_AIC, "elected_anyrepression_v_AIC.tex")

elected_anyrepression_v_lower<-format(round(quantile(elected_anyrepression_v_results$V2,probs=.05), 2))
##write(elected_anyrepression_v_lower, "elected_anyrepression_v_lower.tex")
elected_anyrepression_v_upper<-format(round(quantile(elected_anyrepression_v_results$V2,probs=.95), 2))
##write(elected_anyrepression_v_upper, "elected_anyrepression_v_upper.tex")

elected_constant_anyrepression_e<-format(round(summary(elected_anyrepression_v_reg)$coefficients[1,1], 2))
##write(elected_constant_anyrepression_e, "elected_constant_anyrepression_v.tex")
elected_constant_anyrepression_v_lower<-format(round(quantile(elected_anyrepression_v_results$V1,probs=.05), 2))
##write(elected_constant_anyrepression_v_lower, "elected_constant_anyrepression_v_lower.tex")
elected_constant_anyrepression_v_upper<-format(round(quantile(elected_anyrepression_v_results$V1,probs=.95), 2))
##write(elected_constant_anyrepression_v_upper, "elected_constant_anyrepression_v_upper.tex")

##Overall repression, model 2 --  controls
ar_v_logit_c<-logitCoefficients(dips73, c('anyrepression', 'log_votes', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(ar_v_logit_c))/2

electedc_anyrepression_v_results=as.data.frame(t(matrix(unlist(ar_v_logit_c), ncol=functional_iterations)))

electedc_anyrepression_v_reg<-glm(anyrepression~log_votes+surname+incumbent_orpastelected+dynastic, data=dips73, family="binomial")
electedc_elected_anyrepression_e<-format(round(summary(electedc_anyrepression_v_reg)$coefficients[2,1], 2))
##write(electedc_elected_anyrepression_e, "electedc_elected_anyrepression_v.tex")
electedc_anyrepression_v_AIC<-format(round(summary(electedc_anyrepression_v_reg)$aic, 2))
##write(electedc_anyrepression_v_AIC, "electedc_anyrepression_v_AIC.tex")

electedc_anyrepression_v_lower<-format(round(quantile(electedc_anyrepression_v_results$V2,probs=.05), 2))
##write(electedc_anyrepression_v_lower, "electedc_anyrepression_v_lower.tex")
electedc_anyrepression_v_upper<-format(round(quantile(electedc_anyrepression_v_results$V2,probs=.95), 2))
##write(electedc_anyrepression_v_upper, "electedc_anyrepression_v_upper.tex")

electedc_constant_anyrepression_e<-format(round(summary(electedc_anyrepression_v_reg)$coefficients[1,1], 2))
##write(electedc_constant_anyrepression_e, "electedc_constant_anyrepression_v.tex")
electedc_constant_anyrepression_v_lower<-format(round(quantile(electedc_anyrepression_v_results$V1,probs=.05), 2))
##write(electedc_constant_anyrepression_v_lower, "electedc_constant_anyrepression_v_lower.tex")
electedc_constant_anyrepression_v_upper<-format(round(quantile(electedc_anyrepression_v_results$V1,probs=.95), 2))
##write(electedc_constant_anyrepression_v_upper, "electedc_constant_anyrepression_v_upper.tex")

##Overall repression, model 3 -- full controls
ar_v_logit_pdc<-logitCoefficients(dips73, c('anyrepression', 'log_votes', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(ar_v_logit_pdc))/2

electedpdc_anyrepression_v_results=as.data.frame(t(matrix(unlist(ar_v_logit_pdc), ncol=functional_iterations)))
electedpdc_anyrepression_v_reg<-glm(anyrepression~log_votes+factor(list)+factor(agrupacion)+surname+dynastic+incumbent_orpastelected, data=dips73, family="binomial")

electedpdc_elected_anyrepression_e<-format(round(summary(electedpdc_anyrepression_v_reg)$coefficients[2,1], 2))
##write(electedpdc_elected_anyrepression_e, "electedpdc_elected_anyrepression_v.tex")
electedpdc_anyrepression_v_AIC<-format(round(summary(electedpdc_anyrepression_v_reg)$aic, 2))
##write(electedpdc_anyrepression_v_AIC, "electedpdc_anyrepression_v_AIC.tex")

electedpdc_anyrepression_v_lower<-format(round(quantile(electedpdc_anyrepression_v_results$V2,probs=.05), 2))
##write(electedpdc_anyrepression_v_lower, "electedpdc_anyrepression_v_lower.tex")
electedpdc_anyrepression_v_upper<-format(round(quantile(electedpdc_anyrepression_v_results$V2,probs=.95), 2))
##write(electedpdc_anyrepression_v_upper, "electedpdc_anyrepression_v_upper.tex")

electedpdc_constant_anyrepression_e<-format(round(summary(electedpdc_anyrepression_v_reg)$coefficients[1,1], 2))
##write(electedpdc_constant_anyrepression_e, "electedpdc_constant_anyrepression_v.tex")
electedpdc_constant_anyrepression_v_lower<-format(round(quantile(electedpdc_anyrepression_v_results$V1,probs=.05), 2))
##write(electedpdc_constant_anyrepression_v_lower, "electedpdc_constant_anyrepression_v_lower.tex")
electedpdc_constant_anyrepression_v_upper<-format(round(quantile(electedpdc_anyrepression_v_results$V1,probs=.95), 2))
##write(electedpdc_constant_anyrepression_v_upper, "electedpdc_constant_anyrepression_v_upper.tex")


###Logistic regression, excluding USOPO

##Any repression, model 1 -- no controls
ar_v_logit_noc_nousopo<-logitCoefficients(dips73_nousopo, c('anyrepression', 'log_votes'), iterations)
functional_iterations=length(unlist(ar_v_logit_noc_nousopo))/2

elected_anyrepression_v_logit_results_nousopo=as.data.frame(t(matrix(unlist(ar_v_logit_noc_nousopo), ncol=functional_iterations)))
elected_anyrepression_v_nousopo_reg<-glm(anyrepression~log_votes, data=dips73_nousopo, family="binomial")
elected_elected_anyrepression_v_nousopo<-format(round(summary(elected_anyrepression_v_nousopo_reg)$coefficients[2,1], 2))
##write(elected_elected_anyrepression_v_nousopo, "elected_elected_anyrepression_v_nousopo.tex")
elected_anyrepression_v_nousopo_AIC<-format(round(summary(elected_anyrepression_v_nousopo_reg)$aic, 2))
##write(elected_anyrepression_v_nousopo_AIC, "elected_anyrepression_v_nousopo_AIC.tex")

elected_anyrepression_v_lower_nousopo<-format(round(quantile(elected_anyrepression_v_logit_results_nousopo$V2,probs=.05), 2))
##write(elected_anyrepression_v_lower_nousopo, "elected_anyrepression_v_lower_nousopo.tex")
elected_anyrepression_v_upper_nousopo<-format(round(quantile(elected_anyrepression_v_logit_results_nousopo$V2,probs=.95), 2))
##write(elected_anyrepression_v_upper_nousopo, "elected_anyrepression_v_upper_nousopo.tex")

elected_constant_anyrepression_v_nousopo<-format(round(summary(elected_anyrepression_v_nousopo_reg)$coefficients[1,1], 2))
##write(elected_constant_anyrepression_v_nousopo, "elected_constant_anyrepression_v_nousopo.tex")
elected_constant_anyrepression_v_lower_nousopo<-format(round(quantile(elected_anyrepression_v_logit_results_nousopo$V1,probs=.05), 2))
##write(elected_constant_anyrepression_v_lower_nousopo, "elected_constant_anyrepression_v_lower_nousopo.tex")
elected_constant_anyrepression_v_upper_nousopo<-format(round(quantile(elected_anyrepression_v_logit_results_nousopo$V1,probs=.95), 2))
##write(elected_constant_anyrepression_v_upper_nousopo, "elected_constant_anyrepression_v_upper_nousopo.tex")

##Any repression, model 2 -- party controls
ar_v_logit_c_nousopo<-logitCoefficients(dips73_nousopo, c('anyrepression', 'log_votes', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(ar_v_logit_c_nousopo))/2

electedc_anyrepression_v_logit_results_nousopo=as.data.frame(t(matrix(unlist(ar_v_logit_c_nousopo), ncol=functional_iterations)))

electedc_anyrepression_v_nousopo_reg<-glm(anyrepression~log_votes+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo, family="binomial")
electedc_elected_anyrepression_v_nousopo<-format(round(summary(electedc_anyrepression_v_nousopo_reg)$coefficients[2,1], 2))
##write(electedc_elected_anyrepression_v_nousopo, "electedc_elected_anyrepression_v_nousopo.tex")
electedc_anyrepression_v_nousopo_AIC<-format(round(summary(electedc_anyrepression_v_nousopo_reg)$aic, 2))
##write(electedc_anyrepression_v_nousopo_AIC, "electedc_anyrepression_v_nousopo_AIC.tex")

electedc_anyrepression_v_lower_nousopo<-format(round(quantile(electedc_anyrepression_v_logit_results_nousopo$V2,probs=.05), 2))
##write(electedc_anyrepression_v_lower_nousopo, "electedc_anyrepression_v_lower_nousopo.tex")
electedc_anyrepression_v_upper_nousopo<-format(round(quantile(electedc_anyrepression_v_logit_results_nousopo$V2,probs=.95), 2))
##write(electedc_anyrepression_v_upper_nousopo, "electedc_anyrepression_v_upper_nousopo.tex")

electedc_constant_anyrepression_v_nousopo<-format(round(summary(electedc_anyrepression_v_nousopo_reg)$coefficients[1,1], 2))
##write(electedc_constant_anyrepression_v_nousopo, "electedc_constant_anyrepression_v_nousopo.tex")
electedc_constant_anyrepression_v_lower_nousopo<-format(round(quantile(electedc_anyrepression_v_logit_results_nousopo$V1,probs=.05), 2))
##write(electedc_constant_anyrepression_v_lower_nousopo, "electedc_constant_anyrepression_v_lower_nousopo.tex")
electedc_constant_anyrepression_v_upper_nousopo<-format(round(quantile(electedc_anyrepression_v_logit_results_nousopo$V1,probs=.95), 2))
##write(electedc_constant_anyrepression_v_upper_nousopo, "electedc_constant_anyrepression_v_upper_nousopo.tex")

##Any repression -- all covariates
ar_v_logit_pdc_nousopo<-logitCoefficients(dips73_nousopo, c('anyrepression', 'log_votes', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(ar_v_logit_pdc_nousopo))/2

electedpdc_anyrepression_v_logit_results_nousopo=as.data.frame(t(matrix(unlist(ar_v_logit_pdc_nousopo), ncol=functional_iterations)))

electedpdc_anyrepression_v_nousopo_reg<-glm(anyrepression~log_votes+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo, family="binomial")
electedpdc_elected_anyrepression_v_nousopo<-format(round(summary(electedpdc_anyrepression_v_nousopo_reg)$coefficients[2,1], 2))
##write(electedpdc_elected_anyrepression_v_nousopo, "electedpdc_elected_anyrepression_v_nousopo.tex")
electedpdc_anyrepression_v_nousopo_AIC<-format(round(summary(electedpdc_anyrepression_v_nousopo_reg)$aic, 2))
##write(electedpdc_anyrepression_v_nousopo_AIC, "electedpdc_anyrepression_v_nousopo_AIC.tex")

electedpdc_anyrepression_v_lower_nousopo<-format(round(quantile(electedpdc_anyrepression_v_logit_results_nousopo$V2,probs=.05), 2))
##write(electedpdc_anyrepression_v_lower_nousopo, "electedpdc_anyrepression_v_lower_nousopo.tex")
electedpdc_anyrepression_v_upper_nousopo<-format(round(quantile(electedpdc_anyrepression_v_logit_results_nousopo$V2,probs=.95), 2))
##write(electedpdc_anyrepression_v_upper_nousopo, "electedpdc_anyrepression_v_upper_nousopo.tex")

electedpdc_constant_anyrepression_v_nousopo<-format(round(summary(electedpdc_anyrepression_v_nousopo_reg)$coefficients[1,1], 2))
##write(electedpdc_constant_anyrepression_v_nousopo, "electedpdc_constant_anyrepression_v_nousopo.tex")
electedpdc_constant_anyrepression_v_lower_nousopo<-format(round(quantile(electedpdc_anyrepression_v_logit_results_nousopo$V1,probs=.05), 2))
##write(electedpdc_constant_anyrepression_v_lower_nousopo, "electedpdc_constant_anyrepression_v_lower_nousopo.tex")
electedpdc_constant_anyrepression_v_upper_nousopo<-format(round(quantile(electedpdc_anyrepression_v_logit_results_nousopo$V1,probs=.95), 2))
##write(electedpdc_constant_anyrepression_v_upper_nousopo, "electedpdc_constant_anyrepression_v_upper_nousopo.tex")



##Any repression, model 1 -- no controls
ar_v_logit_noc_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo, c('anyrepression', 'log_votes'), iterations)
functional_iterations=length(unlist(ar_v_logit_noc_leftDC_nousopo))/2

elected_anyrepression_v_logit_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(ar_v_logit_noc_leftDC_nousopo), ncol=functional_iterations)))
elected_anyrepression_v_leftDC_nousopo_reg<-glm(anyrepression~log_votes, data=dips73_leftDC_nousopo, family="binomial")
elected_elected_anyrepression_v_leftDC_nousopo<-format(round(summary(elected_anyrepression_v_leftDC_nousopo_reg)$coefficients[2,1], 2))
##write(elected_elected_anyrepression_v_leftDC_nousopo, "elected_elected_anyrepression_v_leftDC_nousopo.tex")
elected_anyrepression_v_leftDC_nousopo_AIC<-format(round(summary(elected_anyrepression_v_leftDC_nousopo_reg)$aic, 2))
##write(elected_anyrepression_v_leftDC_nousopo_AIC, "elected_anyrepression_v_leftDC_nousopo_AIC.tex")

elected_anyrepression_v_lower_leftDC_nousopo<-format(round(quantile(elected_anyrepression_v_logit_results_leftDC_nousopo$V2,probs=.05), 2))
##write(elected_anyrepression_v_lower_leftDC_nousopo, "elected_anyrepression_v_lower_leftDC_nousopo.tex")
elected_anyrepression_v_upper_leftDC_nousopo<-format(round(quantile(elected_anyrepression_v_logit_results_leftDC_nousopo$V2,probs=.95), 2))
##write(elected_anyrepression_v_upper_leftDC_nousopo, "elected_anyrepression_v_upper_leftDC_nousopo.tex")

elected_constant_anyrepression_v_leftDC_nousopo<-format(round(summary(elected_anyrepression_v_leftDC_nousopo_reg)$coefficients[1,1], 2))
##write(elected_constant_anyrepression_v_leftDC_nousopo, "elected_constant_anyrepression_v_leftDC_nousopo.tex")
elected_constant_anyrepression_v_lower_leftDC_nousopo<-format(round(quantile(elected_anyrepression_v_logit_results_leftDC_nousopo$V1,probs=.05), 2))
##write(elected_constant_anyrepression_v_lower_leftDC_nousopo, "elected_constant_anyrepression_v_lower_leftDC_nousopo.tex")
elected_constant_anyrepression_v_upper_leftDC_nousopo<-format(round(quantile(elected_anyrepression_v_logit_results_leftDC_nousopo$V1,probs=.95), 2))
##write(elected_constant_anyrepression_v_upper_leftDC_nousopo, "elected_constant_anyrepression_v_upper_leftDC_nousopo.tex")

##Any repression, model 2 -- controls
ar_v_logit_c_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo, c('anyrepression', 'log_votes', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(ar_v_logit_c_leftDC_nousopo))/2

electedc_anyrepression_v_logit_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(ar_v_logit_c_leftDC_nousopo), ncol=functional_iterations)))

electedc_anyrepression_v_leftDC_nousopo_reg<-glm(anyrepression~log_votes+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo, family="binomial")
electedc_elected_anyrepression_v_leftDC_nousopo<-format(round(summary(electedc_anyrepression_v_leftDC_nousopo_reg)$coefficients[2,1], 2))
##write(electedc_elected_anyrepression_v_leftDC_nousopo, "electedc_elected_anyrepression_v_leftDC_nousopo.tex")
electedc_anyrepression_v_leftDC_nousopo_AIC<-format(round(summary(electedc_anyrepression_v_leftDC_nousopo_reg)$aic, 2))
##write(electedc_anyrepression_v_leftDC_nousopo_AIC, "electedc_anyrepression_v_leftDC_nousopo_AIC.tex")

electedc_anyrepression_v_lower_leftDC_nousopo<-format(round(quantile(electedc_anyrepression_v_logit_results_leftDC_nousopo$V2,probs=.05), 2))
##write(electedc_anyrepression_v_lower_leftDC_nousopo, "electedc_anyrepression_v_lower_leftDC_nousopo.tex")
electedc_anyrepression_v_upper_leftDC_nousopo<-format(round(quantile(electedc_anyrepression_v_logit_results_leftDC_nousopo$V2,probs=.95), 2))
##write(electedc_anyrepression_v_upper_leftDC_nousopo, "electedc_anyrepression_v_upper_leftDC_nousopo.tex")

electedc_constant_anyrepression_v_leftDC_nousopo<-format(round(summary(electedc_anyrepression_v_leftDC_nousopo_reg)$coefficients[1,1], 2))
##write(electedc_constant_anyrepression_v_leftDC_nousopo, "electedc_constant_anyrepression_v_leftDC_nousopo.tex")
electedc_constant_anyrepression_v_lower_leftDC_nousopo<-format(round(quantile(electedc_anyrepression_v_logit_results_leftDC_nousopo$V1,probs=.05), 2))
##write(electedc_constant_anyrepression_v_lower_leftDC_nousopo, "electedc_constant_anyrepression_v_lower_leftDC_nousopo.tex")
electedc_constant_anyrepression_v_upper_leftDC_nousopo<-format(round(quantile(electedc_anyrepression_v_logit_results_leftDC_nousopo$V1,probs=.95), 2))
##write(electedc_constant_anyrepression_v_upper_leftDC_nousopo, "electedc_constant_anyrepression_v_upper_leftDC_nousopo.tex")


##Any repression, all covariates
ar_v_logit_pdc_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo, c('anyrepression', 'log_votes', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(ar_v_logit_pdc_leftDC_nousopo))/2

electedpdc_anyrepression_v_logit_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(ar_v_logit_pdc_leftDC_nousopo), ncol=functional_iterations)))

electedpdc_anyrepression_v_leftDC_nousopo_reg<-glm(anyrepression~log_votes+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo, family="binomial")
electedpdc_elected_anyrepression_v_leftDC_nousopo<-format(round(summary(electedpdc_anyrepression_v_leftDC_nousopo_reg)$coefficients[2,1], 2))
##write(electedpdc_elected_anyrepression_v_leftDC_nousopo, "electedpdc_elected_anyrepression_v_leftDC_nousopo.tex")
electedpdc_anyrepression_v_leftDC_nousopo_AIC<-format(round(summary(electedpdc_anyrepression_v_leftDC_nousopo_reg)$aic, 2))
##write(electedpdc_anyrepression_v_leftDC_nousopo_AIC, "electedpdc_anyrepression_v_leftDC_nousopo_AIC.tex")

electedpdc_anyrepression_v_lower_leftDC_nousopo<-format(round(quantile(electedpdc_anyrepression_v_logit_results_leftDC_nousopo$V2,probs=.05), 2))
##write(electedpdc_anyrepression_v_lower_leftDC_nousopo, "electedpdc_anyrepression_v_lower_leftDC_nousopo.tex")
electedpdc_anyrepression_v_upper_leftDC_nousopo<-format(round(quantile(electedpdc_anyrepression_v_logit_results_leftDC_nousopo$V2,probs=.95), 2))
##write(electedpdc_anyrepression_v_upper_leftDC_nousopo, "electedpdc_anyrepression_v_upper_leftDC_nousopo.tex")

electedpdc_constant_anyrepression_v_leftDC_nousopo<-format(round(summary(electedpdc_anyrepression_v_leftDC_nousopo_reg)$coefficients[1,1], 2))
##write(electedpdc_constant_anyrepression_v_leftDC_nousopo, "electedpdc_constant_anyrepression_v_leftDC_nousopo.tex")
electedpdc_constant_anyrepression_v_lower_leftDC_nousopo<-format(round(quantile(electedpdc_anyrepression_v_logit_results_leftDC_nousopo$V1,probs=.05), 2))
##write(electedpdc_constant_anyrepression_v_lower_leftDC_nousopo, "electedpdc_constant_anyrepression_v_lower_leftDC_nousopo.tex")
electedpdc_constant_anyrepression_v_upper_leftDC_nousopo<-format(round(quantile(electedpdc_anyrepression_v_logit_results_leftDC_nousopo$V1,probs=.95), 2))
##write(electedpdc_constant_anyrepression_v_upper_leftDC_nousopo, "electedpdc_constant_anyrepression_v_upper_leftDC_nousopo.tex")



######################################################################
#########OLS results, tables B7 and B8################################
######################################################################
###no controls, any repression
ar_ols_noc<-OLS_Coefficients(dips73, c('anyrepression', 'vote_perc'), iterations)

ols_anyrepression_nc<-lm(anyrepression~vote_perc, data=dips73)
ols_anyrepression_nc_rsquared<-summary(ols_anyrepression_nc)$r.squared
##write(format(round(ols_anyrepression_nc_rsquared, 3)), 'ols_anyrepression_nc_rsquared.tex')

ols_anyrepression_nc_results=as.data.frame(t(matrix(unlist(ar_ols_noc), ncol=iterations)))
ols_anyrepression_nc_lower<-format(round(quantile(ols_anyrepression_nc_results$V2, .025), 3))
ols_anyrepression_nc_upper<-format(round(quantile(ols_anyrepression_nc_results$V2, .95), 3))
ols_anyrepression_nc_coef<-format(round(ols_anyrepression_nc$coefficients[2], 2))

ols_anyrepression_nc_lower_constant<-format(round(quantile(ols_anyrepression_nc_results$V1, .05), 3))
ols_anyrepression_nc_upper_constant<-format(round(quantile(ols_anyrepression_nc_results$V1, .95), 3))
ols_anyrepression_nc_coef_constant<-format(round(ols_anyrepression_nc$coefficients[1], 2))

##write(ols_anyrepression_nc_coef, 'ols_anyrepression_nc_coef.tex')
##write(ols_anyrepression_nc_lower, 'ols_anyrepression_nc_lower.tex')
##write(ols_anyrepression_nc_upper, 'ols_anyrepression_nc_upper.tex')

##write(ols_anyrepression_nc_coef_constant, 'ols_anyrepression_nc_coef_constant.tex')
##write(ols_anyrepression_nc_lower_constant, 'ols_anyrepression_nc_lower_constant.tex')
##write(ols_anyrepression_nc_upper_constant, 'ols_anyrepression_nc_upper_constant.tex')

###OLS results, controls, any repression
ar_ols_c<-OLS_Coefficients(dips73, c('anyrepression', 'vote_perc', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)

ols_anyrepression_c<-lm(anyrepression~vote_perc+surname+incumbent_orpastelected+dynastic, data=dips73)
ols_anyrepression_c_rsquared<-summary(ols_anyrepression_c)$r.squared
##write(format(round(ols_anyrepression_c_rsquared, 2)), 'ols_anyrepression_c_rsquared.tex')

ols_anyrepression_c_results=as.data.frame(t(matrix(unlist(ar_ols_c), ncol=iterations)))
ols_anyrepression_c_lower<-format(round(quantile(ols_anyrepression_c_results$V2, .05), 3))
ols_anyrepression_c_upper<-format(round(quantile(ols_anyrepression_c_results$V2, .95), 3))
ols_anyrepression_c_coef<-format(round(ols_anyrepression_c$coefficients[2], 3))

ols_anyrepression_c_lower_constant<-format(round(quantile(ols_anyrepression_c_results$V1, .05), 3))
ols_anyrepression_c_upper_constant<-format(round(quantile(ols_anyrepression_c_results$V1, .95), 3))
ols_anyrepression_c_coef_constant<-format(round(ols_anyrepression_c$coefficients[1], 3))

##write(ols_anyrepression_c_coef, 'ols_anyrepression_c_coef.tex')
##write(ols_anyrepression_c_lower, 'ols_anyrepression_c_lower.tex')
##write(ols_anyrepression_c_upper, 'ols_anyrepression_c_upper.tex')

##write(ols_anyrepression_c_coef_constant, 'ols_anyrepression_c_coef_constant.tex')
##write(ols_anyrepression_c_lower_constant, 'ols_anyrepression_c_lower_constant.tex')
##write(ols_anyrepression_c_upper_constant, 'ols_anyrepression_c_upper_constant.tex')



###OLS results, controls+district+party, any repression
ar_ols_pdc<-OLS_Coefficients(dips73, c('anyrepression', 'vote_perc', 'surname', 'incumbent_orpastelected', 'dynastic', "Partido", "Distrito"), iterations)

ols_anyrepression_pdc<-lm(anyrepression~elected+surname+incumbent_orpastelected+dynastic+Distrito+Partido, data=dips73)
ols_anyrepression_pdc_results=as.data.frame(t(matrix(unlist(ar_ols_pdc), ncol=iterations)))
ols_anyrepression_pdc_rsquared<-summary(ols_anyrepression_pdc)$r.squared
##write(format(round(ols_anyrepression_pdc_rsquared, 2)), 'ols_anyrepression_pdc_rsquared.tex')

ols_anyrepression_pdc_lower<-format(round(quantile(ols_anyrepression_pdc_results$V2, .05), 3))
ols_anyrepression_pdc_upper<-format(round(quantile(ols_anyrepression_pdc_results$V2, .95), 3))
ols_anyrepression_pdc_coef<-format(round(ols_anyrepression_pdc$coefficients[2], 3))

ols_anyrepression_pdc_lower_constant<-format(round(quantile(ols_anyrepression_pdc_results$V1, .05), 3))
ols_anyrepression_pdc_upper_constant<-format(round(quantile(ols_anyrepression_pdc_results$V1, .95), 3))
ols_anyrepression_pdc_coef_constant<-format(round(ols_anyrepression_pdc$coefficients[1], 3))

##write(ols_anyrepression_pdc_coef, 'ols_anyrepression_pdc_coef.tex')
##write(ols_anyrepression_pdc_lower, 'ols_anyrepression_pdc_lower.tex')
##write(ols_anyrepression_pdc_upper, 'ols_anyrepression_pdc_upper.tex')

##write(ols_anyrepression_pdc_coef_constant, 'ols_anyrepression_pdc_coef_constant.tex')
##write(ols_anyrepression_pdc_lower_constant, 'ols_anyrepression_pdc_lower_constant.tex')
##write(ols_anyrepression_pdc_upper_constant, 'ols_anyrepression_pdc_upper_constant.tex')


###OLS results, no controls, victim
pc_ols_noc<-OLS_Coefficients(dips73, c('victim', 'elected'), iterations)

ols_victim_nc<-lm(victim~elected, data=dips73)
ols_victim_nc_results=as.data.frame(t(matrix(unlist(pc_ols_noc), ncol=iterations)))
ols_victim_nc_rsquared<-summary(ols_victim_nc)$r.squared
##write(format(round(ols_victim_nc_rsquared, 2)), 'ols_victim_nc_rsquared.tex')

ols_victim_nc_lower<-format(round(quantile(ols_victim_nc_results$V2, .05), 2))
ols_victim_nc_upper<-format(round(quantile(ols_victim_nc_results$V2, .95), 2))
ols_victim_nc_coef<-format(round(ols_victim_nc$coefficients[2], 2))

ols_victim_nc_lower_constant<-format(round(quantile(ols_victim_nc_results$V1, .05), 2))
ols_victim_nc_upper_constant<-format(round(quantile(ols_victim_nc_results$V1, .95), 2))
ols_victim_nc_coef_constant<-format(round(ols_victim_nc$coefficients[1], 2))

##write(ols_victim_nc_coef, 'ols_victim_nc_coef.tex')
##write(ols_victim_nc_lower, 'ols_victim_nc_lower.tex')
##write(ols_victim_nc_upper, 'ols_victim_nc_upper.tex')

##write(ols_victim_nc_coef_constant, 'ols_victim_nc_coef_constant.tex')
##write(ols_victim_nc_lower_constant, 'ols_victim_nc_lower_constant.tex')
##write(ols_victim_nc_upper_constant, 'ols_victim_nc_upper_constant.tex')

###OLS results, controls, victim
pc_ols_c<-OLS_Coefficients(dips73, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)

ols_victim_c<-lm(victim~elected+surname+incumbent_orpastelected+dynastic, data=dips73)
ols_victim_c_rsquared<-summary(ols_victim_c)$r.squared
##write(format(round(ols_victim_c_rsquared, 2)), 'ols_victim_c_rsquared.tex')

ols_victim_c_results=as.data.frame(t(matrix(unlist(pc_ols_c), ncol=iterations)))
ols_victim_c_lower<-format(round(quantile(ols_victim_c_results$V2, .05), 2))
ols_victim_c_upper<-format(round(quantile(ols_victim_c_results$V2, .95), 2))
ols_victim_c_coef<-format(round(ols_victim_c$coefficients[2], 2))

ols_victim_c_lower_constant<-format(round(quantile(ols_victim_c_results$V1, .05), 2))
ols_victim_c_upper_constant<-format(round(quantile(ols_victim_c_results$V1, .95), 2))
ols_victim_c_coef_constant<-format(round(ols_victim_c$coefficients[1], 2))

##write(ols_victim_c_coef, 'ols_victim_c_coef.tex')
##write(ols_victim_c_lower, 'ols_victim_c_lower.tex')
##write(ols_victim_c_upper, 'ols_victim_c_upper.tex')

##write(ols_victim_c_coef_constant, 'ols_victim_c_coef_constant.tex')
##write(ols_victim_c_lower_constant, 'ols_victim_c_lower_constant.tex')
##write(ols_victim_c_upper_constant, 'ols_victim_c_upper_constant.tex')

###OLS results, controls+district+party, victim
pc_ols_pdc<-OLS_Coefficients(dips73, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic', 'Partido', 'Distrito'), iterations)

ols_victim_pdc<-lm(victim~elected+surname+incumbent_orpastelected+dynastic+Distrito+Partido, data=dips73)
ols_victim_pdc_rsquared<-summary(ols_victim_pdc)$r.squared
##write(format(round(ols_victim_pdc_rsquared, 2)), 'ols_victim_pdc_rsquared.tex')

ols_victim_pdc_results=as.data.frame(t(matrix(unlist(pc_ols_pdc), ncol=iterations)))
ols_victim_pdc_lower<-format(round(quantile(ols_victim_pdc_results$V2, .05), 2))
ols_victim_pdc_upper<-format(round(quantile(ols_victim_pdc_results$V2, .95), 2))
ols_victim_pdc_coef<-format(round(ols_victim_pdc$coefficients[2], 2))

ols_victim_pdc_lower_constant<-format(round(quantile(ols_victim_pdc_results$V1, .05), 2))
ols_victim_pdc_upper_constant<-format(round(quantile(ols_victim_pdc_results$V1, .95), 2))
ols_victim_pdc_coef_constant<-format(round(ols_victim_pdc$coefficients[1], 2))

##write(ols_victim_pdc_coef, 'ols_victim_pdc_coef.tex')
##write(ols_victim_pdc_lower, 'ols_victim_pdc_lower.tex')
##write(ols_victim_pdc_upper, 'ols_victim_pdc_upper.tex')

##write(ols_victim_pdc_coef_constant, 'ols_victim_pdc_coef_constant.tex')
##write(ols_victim_pdc_lower_constant, 'ols_victim_pdc_lower_constant.tex')
##write(ols_victim_pdc_upper_constant, 'ols_victim_pdc_upper_constant.tex')


###########Excluding USOPO
ar_ols_noc_nousopo<-OLS_Coefficients(dips73_nousopo, c('anyrepression', 'vote_perc'), iterations)

ols_nousopo_anyrepression_nc<-lm(anyrepression~vote_perc, data=dips73_nousopo)
ols_nousopo_anyrepression_nc_rsquared<-summary(ols_nousopo_anyrepression_nc)$r.squared
##write(format(round(ols_nousopo_anyrepression_nc_rsquared, 2)), 'ols_nousopo_anyrepression_nc_rsquared.tex')

ols_nousopo_anyrepression_nc_results=as.data.frame(t(matrix(unlist(ar_ols_noc_nousopo), ncol=iterations)))
ols_nousopo_anyrepression_nc_lower<-format(round(quantile(ols_nousopo_anyrepression_nc_results$V2, .05), 3))
ols_nousopo_anyrepression_nc_upper<-format(round(quantile(ols_nousopo_anyrepression_nc_results$V2, .95), 3))
ols_nousopo_anyrepression_nc_coef<-format(round(ols_nousopo_anyrepression_nc$coefficients[2], 3))

ols_nousopo_anyrepression_nc_lower_constant<-format(round(quantile(ols_nousopo_anyrepression_nc_results$V1, .05), 3))
ols_nousopo_anyrepression_nc_upper_constant<-format(round(quantile(ols_nousopo_anyrepression_nc_results$V1, .95), 3))
ols_nousopo_anyrepression_nc_coef_constant<-format(round(ols_nousopo_anyrepression_nc$coefficients[1], 3))

##write(ols_nousopo_anyrepression_nc_coef, 'ols_nousopo_anyrepression_nc_coef.tex')
##write(ols_nousopo_anyrepression_nc_lower, 'ols_nousopo_anyrepression_nc_lower.tex')
##write(ols_nousopo_anyrepression_nc_upper, 'ols_nousopo_anyrepression_nc_upper.tex')

##write(ols_nousopo_anyrepression_nc_coef_constant, 'ols_nousopo_anyrepression_nc_coef_constant.tex')
##write(ols_nousopo_anyrepression_nc_lower_constant, 'ols_nousopo_anyrepression_nc_lower_constant.tex')
##write(ols_nousopo_anyrepression_nc_upper_constant, 'ols_nousopo_anyrepression_nc_upper_constant.tex')

###OLS results, controls, any repression
ar_ols_c_nousopo<-OLS_Coefficients(dips73_nousopo, c('anyrepression', 'vote_perc', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)

ols_nousopo_anyrepression_c<-lm(anyrepression~vote_perc+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo)
ols_nousopo_anyrepression_c_rsquared<-summary(ols_nousopo_anyrepression_c)$r.squared
##write(format(round(ols_nousopo_anyrepression_c_rsquared, 2)), 'ols_nousopo_anyrepression_c_rsquared.tex')

ols_nousopo_anyrepression_c_results=as.data.frame(t(matrix(unlist(ar_ols_c_nousopo), ncol=iterations)))
ols_nousopo_anyrepression_c_lower<-format(round(quantile(ols_nousopo_anyrepression_c_results$V2, .05), 3))
ols_nousopo_anyrepression_c_upper<-format(round(quantile(ols_nousopo_anyrepression_c_results$V2, .95), 3))
ols_nousopo_anyrepression_c_coef<-format(round(ols_nousopo_anyrepression_c$coefficients[2], 3))

ols_nousopo_anyrepression_c_lower_constant<-format(round(quantile(ols_nousopo_anyrepression_c_results$V1, .05), 3))
ols_nousopo_anyrepression_c_upper_constant<-format(round(quantile(ols_nousopo_anyrepression_c_results$V1, .95), 3))
ols_nousopo_anyrepression_c_coef_constant<-format(round(ols_nousopo_anyrepression_c$coefficients[1], 3))

##write(ols_nousopo_anyrepression_c_coef, 'ols_nousopo_anyrepression_c_coef.tex')
##write(ols_nousopo_anyrepression_c_lower, 'ols_nousopo_anyrepression_c_lower.tex')
##write(ols_nousopo_anyrepression_c_upper, 'ols_nousopo_anyrepression_c_upper.tex')

##write(ols_nousopo_anyrepression_c_coef_constant, 'ols_nousopo_anyrepression_c_coef_constant.tex')
##write(ols_nousopo_anyrepression_c_lower_constant, 'ols_nousopo_anyrepression_c_lower_constant.tex')
##write(ols_nousopo_anyrepression_c_upper_constant, 'ols_nousopo_anyrepression_c_upper_constant.tex')

###OLS results, controls+district+party, any repression
ar_ols_pdc_nousopo<-OLS_Coefficients(dips73_nousopo, c('anyrepression', 'vote_perc', 'surname', 'incumbent_orpastelected', 'dynastic', 'Partido', 'Distrito'), iterations)

ols_nousopo_anyrepression_pdc<-lm(anyrepression~vote_perc+surname+incumbent_orpastelected+dynastic+Distrito+Partido, data=dips73_nousopo)
ols_nousopo_anyrepression_pdc_results=as.data.frame(t(matrix(unlist(ar_ols_pdc_nousopo), ncol=iterations)))
ols_nousopo_anyrepression_pdc_rsquared<-summary(ols_nousopo_anyrepression_pdc)$r.squared
##write(format(round(ols_nousopo_anyrepression_pdc_rsquared, 2)), 'ols_nousopo_anyrepression_pdc_rsquared.tex')

ols_nousopo_anyrepression_pdc_lower<-format(round(quantile(ols_nousopo_anyrepression_pdc_results$V2, .05), 3))
ols_nousopo_anyrepression_pdc_upper<-format(round(quantile(ols_nousopo_anyrepression_pdc_results$V2, .95), 3))
ols_nousopo_anyrepression_pdc_coef<-format(round(ols_nousopo_anyrepression_pdc$coefficients[2], 3))

ols_nousopo_anyrepression_pdc_lower_constant<-format(round(quantile(ols_nousopo_anyrepression_pdc_results$V1, .05), 3))
ols_nousopo_anyrepression_pdc_upper_constant<-format(round(quantile(ols_nousopo_anyrepression_pdc_results$V1, .95), 3))
ols_nousopo_anyrepression_pdc_coef_constant<-format(round(ols_nousopo_anyrepression_pdc$coefficients[1], 3))

##write(ols_nousopo_anyrepression_pdc_coef, 'ols_nousopo_anyrepression_pdc_coef.tex')
##write(ols_nousopo_anyrepression_pdc_lower, 'ols_nousopo_anyrepression_pdc_lower.tex')
##write(ols_nousopo_anyrepression_pdc_upper, 'ols_nousopo_anyrepression_pdc_upper.tex')

##write(ols_nousopo_anyrepression_pdc_coef_constant, 'ols_nousopo_anyrepression_pdc_coef_constant.tex')
##write(ols_nousopo_anyrepression_pdc_lower_constant, 'ols_nousopo_anyrepression_pdc_lower_constant.tex')
##write(ols_nousopo_anyrepression_pdc_upper_constant, 'ols_nousopo_anyrepression_pdc_upper_constant.tex')


###OLS results, no controls, victim
pc_ols_noc_nousopo<-OLS_Coefficients(dips73_nousopo, c('victim', 'elected'), iterations)

ols_nousopo_victim_nc<-lm(victim~elected, data=dips73_nousopo)
ols_nousopo_victim_nc_results=as.data.frame(t(matrix(unlist(pc_ols_noc_nousopo), ncol=iterations)))
ols_nousopo_victim_nc_rsquared<-summary(ols_nousopo_victim_nc)$r.squared
##write(format(round(ols_nousopo_victim_nc_rsquared, 2)), 'ols_nousopo_victim_nc_rsquared.tex')

ols_nousopo_victim_nc_lower<-format(round(quantile(ols_nousopo_victim_nc_results$V2, .05), 2))
ols_nousopo_victim_nc_upper<-format(round(quantile(ols_nousopo_victim_nc_results$V2, .95), 2))
ols_nousopo_victim_nc_coef<-format(round(ols_nousopo_victim_nc$coefficients[2], 2))

ols_nousopo_victim_nc_lower_constant<-format(round(quantile(ols_nousopo_victim_nc_results$V1, .05), 2))
ols_nousopo_victim_nc_upper_constant<-format(round(quantile(ols_nousopo_victim_nc_results$V1, .95), 2))
ols_nousopo_victim_nc_coef_constant<-format(round(ols_nousopo_victim_nc$coefficients[1], 2))

##write(ols_nousopo_victim_nc_coef, 'ols_nousopo_victim_nc_coef.tex')
##write(ols_nousopo_victim_nc_lower, 'ols_nousopo_victim_nc_lower.tex')
##write(ols_nousopo_victim_nc_upper, 'ols_nousopo_victim_nc_upper.tex')

##write(ols_nousopo_victim_nc_coef_constant, 'ols_nousopo_victim_nc_coef_constant.tex')
##write(ols_nousopo_victim_nc_lower_constant, 'ols_nousopo_victim_nc_lower_constant.tex')
##write(ols_nousopo_victim_nc_upper_constant, 'ols_nousopo_victim_nc_upper_constant.tex')

###OLS results, controls, victim
pc_ols_c_nousopo<-OLS_Coefficients(dips73_nousopo, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)

ols_nousopo_victim_c<-lm(victim~elected+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo)
ols_nousopo_victim_c_rsquared<-summary(ols_nousopo_victim_c)$r.squared
##write(format(round(ols_nousopo_victim_c_rsquared, 2)), 'ols_nousopo_victim_c_rsquared.tex')

ols_nousopo_victim_c_results=as.data.frame(t(matrix(unlist(pc_ols_c_nousopo), ncol=iterations)))
ols_nousopo_victim_c_lower<-format(round(quantile(ols_nousopo_victim_c_results$V2, .05), 2))
ols_nousopo_victim_c_upper<-format(round(quantile(ols_nousopo_victim_c_results$V2, .95), 2))
ols_nousopo_victim_c_coef<-format(round(ols_nousopo_victim_c$coefficients[2], 2))

ols_nousopo_victim_c_lower_constant<-format(round(quantile(ols_nousopo_victim_c_results$V1, .05), 2))
ols_nousopo_victim_c_upper_constant<-format(round(quantile(ols_nousopo_victim_c_results$V1, .95), 2))
ols_nousopo_victim_c_coef_constant<-format(round(ols_nousopo_victim_c$coefficients[1], 2))

##write(ols_nousopo_victim_c_coef, 'ols_nousopo_victim_c_coef.tex')
##write(ols_nousopo_victim_c_lower, 'ols_nousopo_victim_c_lower.tex')
##write(ols_nousopo_victim_c_upper, 'ols_nousopo_victim_c_upper.tex')

##write(ols_nousopo_victim_c_coef_constant, 'ols_nousopo_victim_c_coef_constant.tex')
##write(ols_nousopo_victim_c_lower_constant, 'ols_nousopo_victim_c_lower_constant.tex')
##write(ols_nousopo_victim_c_upper_constant, 'ols_nousopo_victim_c_upper_constant.tex')

###OLS results, controls+district+party, victim
pc_ols_pdc_nousopo<-OLS_Coefficients(dips73_nousopo, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic', 'Partido', 'Distrito'), iterations)

ols_nousopo_victim_pdc<-lm(victim~elected+surname+incumbent_orpastelected+dynastic+Distrito+Partido, data=dips73_nousopo)
ols_nousopo_victim_pdc_rsquared<-summary(ols_nousopo_victim_pdc)$r.squared
##write(format(round(ols_nousopo_victim_pdc_rsquared, 2)), 'ols_nousopo_victim_pdc_rsquared.tex')

ols_nousopo_victim_pdc_results=as.data.frame(t(matrix(unlist(pc_ols_pdc_nousopo), ncol=iterations)))
ols_nousopo_victim_pdc_lower<-format(round(quantile(ols_nousopo_victim_pdc_results$V2, .05), 2))
ols_nousopo_victim_pdc_upper<-format(round(quantile(ols_nousopo_victim_pdc_results$V2, .95), 2))
ols_nousopo_victim_pdc_coef<-format(round(ols_nousopo_victim_pdc$coefficients[2], 2))

ols_nousopo_victim_pdc_lower_constant<-format(round(quantile(ols_nousopo_victim_pdc_results$V1, .05), 2))
ols_nousopo_victim_pdc_upper_constant<-format(round(quantile(ols_nousopo_victim_pdc_results$V1, .95), 2))
ols_nousopo_victim_pdc_coef_constant<-format(round(ols_nousopo_victim_pdc$coefficients[1], 2))

##write(ols_nousopo_victim_pdc_coef, 'ols_nousopo_victim_pdc_coef.tex')
##write(ols_nousopo_victim_pdc_lower, 'ols_nousopo_victim_pdc_lower.tex')
##write(ols_nousopo_victim_pdc_upper, 'ols_nousopo_victim_pdc_upper.tex')

##write(ols_nousopo_victim_pdc_coef_constant, 'ols_nousopo_victim_pdc_coef_constant.tex')
##write(ols_nousopo_victim_pdc_lower_constant, 'ols_nousopo_victim_pdc_lower_constant.tex')
##write(ols_nousopo_victim_pdc_upper_constant, 'ols_nousopo_victim_pdc_upper_constant.tex')




###########Excluding leftDC_nousopo
ar_ols_noc_leftDC_nousopo<-OLS_Coefficients(dips73_leftDC_nousopo, c('anyrepression', 'vote_perc'), iterations)

ols_leftDC_nousopo_anyrepression_nc<-lm(anyrepression~vote_perc, data=dips73_leftDC_nousopo)
ols_leftDC_nousopo_anyrepression_nc_rsquared<-summary(ols_leftDC_nousopo_anyrepression_nc)$r.squared
##write(format(round(ols_leftDC_nousopo_anyrepression_nc_rsquared, 2)), 'ols_leftDC_nousopo_anyrepression_nc_rsquared.tex')

ols_leftDC_nousopo_anyrepression_nc_results=as.data.frame(t(matrix(unlist(ar_ols_noc_leftDC_nousopo), ncol=iterations)))
ols_leftDC_nousopo_anyrepression_nc_lower<-format(round(quantile(ols_leftDC_nousopo_anyrepression_nc_results$V2, .05), 3))
ols_leftDC_nousopo_anyrepression_nc_upper<-format(round(quantile(ols_leftDC_nousopo_anyrepression_nc_results$V2, .95), 3))
ols_leftDC_nousopo_anyrepression_nc_coef<-format(round(ols_leftDC_nousopo_anyrepression_nc$coefficients[2], 3))

ols_leftDC_nousopo_anyrepression_nc_lower_constant<-format(round(quantile(ols_leftDC_nousopo_anyrepression_nc_results$V1, .05), 3))
ols_leftDC_nousopo_anyrepression_nc_upper_constant<-format(round(quantile(ols_leftDC_nousopo_anyrepression_nc_results$V1, .95), 3))
ols_leftDC_nousopo_anyrepression_nc_coef_constant<-format(round(ols_leftDC_nousopo_anyrepression_nc$coefficients[1], 3))

##write(ols_leftDC_nousopo_anyrepression_nc_coef, 'ols_leftDC_nousopo_anyrepression_nc_coef.tex')
##write(ols_leftDC_nousopo_anyrepression_nc_lower, 'ols_leftDC_nousopo_anyrepression_nc_lower.tex')
##write(ols_leftDC_nousopo_anyrepression_nc_upper, 'ols_leftDC_nousopo_anyrepression_nc_upper.tex')

##write(ols_leftDC_nousopo_anyrepression_nc_coef_constant, 'ols_leftDC_nousopo_anyrepression_nc_coef_constant.tex')
##write(ols_leftDC_nousopo_anyrepression_nc_lower_constant, 'ols_leftDC_nousopo_anyrepression_nc_lower_constant.tex')
##write(ols_leftDC_nousopo_anyrepression_nc_upper_constant, 'ols_leftDC_nousopo_anyrepression_nc_upper_constant.tex')

###OLS results, controls, any repression
ar_ols_c_leftDC_nousopo<-OLS_Coefficients(dips73_leftDC_nousopo, c('anyrepression', 'vote_perc', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)

ols_leftDC_nousopo_anyrepression_c<-lm(anyrepression~vote_perc+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo)
ols_leftDC_nousopo_anyrepression_c_rsquared<-summary(ols_leftDC_nousopo_anyrepression_c)$r.squared
##write(format(round(ols_leftDC_nousopo_anyrepression_c_rsquared, 2)), 'ols_leftDC_nousopo_anyrepression_c_rsquared.tex')

ols_leftDC_nousopo_anyrepression_c_results=as.data.frame(t(matrix(unlist(ar_ols_c_leftDC_nousopo), ncol=iterations)))
ols_leftDC_nousopo_anyrepression_c_lower<-format(round(quantile(ols_leftDC_nousopo_anyrepression_c_results$V2, .05), 3))
ols_leftDC_nousopo_anyrepression_c_upper<-format(round(quantile(ols_leftDC_nousopo_anyrepression_c_results$V2, .95), 3))
ols_leftDC_nousopo_anyrepression_c_coef<-format(round(ols_leftDC_nousopo_anyrepression_c$coefficients[2], 3))

ols_leftDC_nousopo_anyrepression_c_lower_constant<-format(round(quantile(ols_leftDC_nousopo_anyrepression_c_results$V1, .05), 3))
ols_leftDC_nousopo_anyrepression_c_upper_constant<-format(round(quantile(ols_leftDC_nousopo_anyrepression_c_results$V1, .95), 3))
ols_leftDC_nousopo_anyrepression_c_coef_constant<-format(round(ols_leftDC_nousopo_anyrepression_c$coefficients[1], 3))

##write(ols_leftDC_nousopo_anyrepression_c_coef, 'ols_leftDC_nousopo_anyrepression_c_coef.tex')
##write(ols_leftDC_nousopo_anyrepression_c_lower, 'ols_leftDC_nousopo_anyrepression_c_lower.tex')
##write(ols_leftDC_nousopo_anyrepression_c_upper, 'ols_leftDC_nousopo_anyrepression_c_upper.tex')

##write(ols_leftDC_nousopo_anyrepression_c_coef_constant, 'ols_leftDC_nousopo_anyrepression_c_coef_constant.tex')
##write(ols_leftDC_nousopo_anyrepression_c_lower_constant, 'ols_leftDC_nousopo_anyrepression_c_lower_constant.tex')
##write(ols_leftDC_nousopo_anyrepression_c_upper_constant, 'ols_leftDC_nousopo_anyrepression_c_upper_constant.tex')

###OLS results, controls+district+party, any repression
ar_ols_pdc_leftDC_nousopo<-OLS_Coefficients(dips73_leftDC_nousopo, c('anyrepression', 'vote_perc', 'surname', 'incumbent_orpastelected', 'dynastic', "Distrito", "Partido"), iterations)

ols_leftDC_nousopo_anyrepression_pdc<-lm(anyrepression~vote_perc+surname+incumbent_orpastelected+dynastic+Distrito+Partido, data=dips73_leftDC_nousopo)
ols_leftDC_nousopo_anyrepression_pdc_results=as.data.frame(t(matrix(unlist(ar_ols_pdc_leftDC_nousopo), ncol=iterations)))
ols_leftDC_nousopo_anyrepression_pdc_rsquared<-summary(ols_leftDC_nousopo_anyrepression_pdc)$r.squared
##write(format(round(ols_leftDC_nousopo_anyrepression_pdc_rsquared, 2)), 'ols_leftDC_nousopo_anyrepression_pdc_rsquared.tex')

ols_leftDC_nousopo_anyrepression_pdc_lower<-format(round(quantile(ols_leftDC_nousopo_anyrepression_pdc_results$V2, .05), 3))
ols_leftDC_nousopo_anyrepression_pdc_upper<-format(round(quantile(ols_leftDC_nousopo_anyrepression_pdc_results$V2, .95), 3))
ols_leftDC_nousopo_anyrepression_pdc_coef<-format(round(ols_leftDC_nousopo_anyrepression_pdc$coefficients[2], 3))

ols_leftDC_nousopo_anyrepression_pdc_lower_constant<-format(round(quantile(ols_leftDC_nousopo_anyrepression_pdc_results$V1, .05), 3))
ols_leftDC_nousopo_anyrepression_pdc_upper_constant<-format(round(quantile(ols_leftDC_nousopo_anyrepression_pdc_results$V1, .95), 3))
ols_leftDC_nousopo_anyrepression_pdc_coef_constant<-format(round(ols_leftDC_nousopo_anyrepression_pdc$coefficients[1], 3))

##write(ols_leftDC_nousopo_anyrepression_pdc_coef, 'ols_leftDC_nousopo_anyrepression_pdc_coef.tex')
##write(ols_leftDC_nousopo_anyrepression_pdc_lower, 'ols_leftDC_nousopo_anyrepression_pdc_lower.tex')
##write(ols_leftDC_nousopo_anyrepression_pdc_upper, 'ols_leftDC_nousopo_anyrepression_pdc_upper.tex')

##write(ols_leftDC_nousopo_anyrepression_pdc_coef_constant, 'ols_leftDC_nousopo_anyrepression_pdc_coef_constant.tex')
##write(ols_leftDC_nousopo_anyrepression_pdc_lower_constant, 'ols_leftDC_nousopo_anyrepression_pdc_lower_constant.tex')
##write(ols_leftDC_nousopo_anyrepression_pdc_upper_constant, 'ols_leftDC_nousopo_anyrepression_pdc_upper_constant.tex')


###OLS results, no controls, victim
pc_ols_noc_leftDC_nousopo<-OLS_Coefficients(dips73_leftDC_nousopo, c('victim', 'elected'), iterations)

ols_leftDC_nousopo_victim_nc<-lm(victim~elected, data=dips73_leftDC_nousopo)
ols_leftDC_nousopo_victim_nc_results=as.data.frame(t(matrix(unlist(pc_ols_noc_leftDC_nousopo), ncol=iterations)))
ols_leftDC_nousopo_victim_nc_rsquared<-summary(ols_leftDC_nousopo_victim_nc)$r.squared
##write(format(round(ols_leftDC_nousopo_victim_nc_rsquared, 2)), 'ols_leftDC_nousopo_victim_nc_rsquared.tex')

ols_leftDC_nousopo_victim_nc_lower<-format(round(quantile(ols_leftDC_nousopo_victim_nc_results$V2, .05), 2))
ols_leftDC_nousopo_victim_nc_upper<-format(round(quantile(ols_leftDC_nousopo_victim_nc_results$V2, .95), 2))
ols_leftDC_nousopo_victim_nc_coef<-format(round(ols_leftDC_nousopo_victim_nc$coefficients[2], 2))

ols_leftDC_nousopo_victim_nc_lower_constant<-format(round(quantile(ols_leftDC_nousopo_victim_nc_results$V1, .05), 2))
ols_leftDC_nousopo_victim_nc_upper_constant<-format(round(quantile(ols_leftDC_nousopo_victim_nc_results$V1, .95), 2))
ols_leftDC_nousopo_victim_nc_coef_constant<-format(round(ols_leftDC_nousopo_victim_nc$coefficients[1], 2))

##write(ols_leftDC_nousopo_victim_nc_coef, 'ols_leftDC_nousopo_victim_nc_coef.tex')
##write(ols_leftDC_nousopo_victim_nc_lower, 'ols_leftDC_nousopo_victim_nc_lower.tex')
##write(ols_leftDC_nousopo_victim_nc_upper, 'ols_leftDC_nousopo_victim_nc_upper.tex')

##write(ols_leftDC_nousopo_victim_nc_coef_constant, 'ols_leftDC_nousopo_victim_nc_coef_constant.tex')
##write(ols_leftDC_nousopo_victim_nc_lower_constant, 'ols_leftDC_nousopo_victim_nc_lower_constant.tex')
##write(ols_leftDC_nousopo_victim_nc_upper_constant, 'ols_leftDC_nousopo_victim_nc_upper_constant.tex')

###OLS results, controls, victim
pc_ols_c_leftDC_nousopo<-OLS_Coefficients(dips73_leftDC_nousopo, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)

ols_leftDC_nousopo_victim_c<-lm(victim~elected+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo)
ols_leftDC_nousopo_victim_c_rsquared<-summary(ols_leftDC_nousopo_victim_c)$r.squared
##write(format(round(ols_leftDC_nousopo_victim_c_rsquared, 2)), 'ols_leftDC_nousopo_victim_c_rsquared.tex')

ols_leftDC_nousopo_victim_c_results=as.data.frame(t(matrix(unlist(pc_ols_c_leftDC_nousopo), ncol=iterations)))
ols_leftDC_nousopo_victim_c_lower<-format(round(quantile(ols_leftDC_nousopo_victim_c_results$V2, .05), 2))
ols_leftDC_nousopo_victim_c_upper<-format(round(quantile(ols_leftDC_nousopo_victim_c_results$V2, .95), 2))
ols_leftDC_nousopo_victim_c_coef<-format(round(ols_leftDC_nousopo_victim_c$coefficients[2], 2))

ols_leftDC_nousopo_victim_c_lower_constant<-format(round(quantile(ols_leftDC_nousopo_victim_c_results$V1, .05), 2))
ols_leftDC_nousopo_victim_c_upper_constant<-format(round(quantile(ols_leftDC_nousopo_victim_c_results$V1, .95), 2))
ols_leftDC_nousopo_victim_c_coef_constant<-format(round(ols_leftDC_nousopo_victim_c$coefficients[1], 2))

##write(ols_leftDC_nousopo_victim_c_coef, 'ols_leftDC_nousopo_victim_c_coef.tex')
##write(ols_leftDC_nousopo_victim_c_lower, 'ols_leftDC_nousopo_victim_c_lower.tex')
##write(ols_leftDC_nousopo_victim_c_upper, 'ols_leftDC_nousopo_victim_c_upper.tex')

##write(ols_leftDC_nousopo_victim_c_coef_constant, 'ols_leftDC_nousopo_victim_c_coef_constant.tex')
##write(ols_leftDC_nousopo_victim_c_lower_constant, 'ols_leftDC_nousopo_victim_c_lower_constant.tex')
##write(ols_leftDC_nousopo_victim_c_upper_constant, 'ols_leftDC_nousopo_victim_c_upper_constant.tex')

###OLS results, controls+district+party, victim
pc_ols_pdc_leftDC_nousopo<-OLS_Coefficients(dips73_leftDC_nousopo, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic', 'Distrito', 'Partido'), iterations)

ols_leftDC_nousopo_victim_pdc<-lm(victim~elected+surname+incumbent_orpastelected+dynastic+Distrito+Partido, data=dips73_leftDC_nousopo)
ols_leftDC_nousopo_victim_pdc_rsquared<-summary(ols_leftDC_nousopo_victim_pdc)$r.squared
##write(format(round(ols_leftDC_nousopo_victim_pdc_rsquared, 2)), 'ols_leftDC_nousopo_victim_pdc_rsquared.tex')

ols_leftDC_nousopo_victim_pdc_results=as.data.frame(t(matrix(unlist(pc_ols_pdc_leftDC_nousopo), ncol=iterations)))
ols_leftDC_nousopo_victim_pdc_lower<-format(round(quantile(ols_leftDC_nousopo_victim_pdc_results$V2, .05), 2))
ols_leftDC_nousopo_victim_pdc_upper<-format(round(quantile(ols_leftDC_nousopo_victim_pdc_results$V2, .95), 2))
ols_leftDC_nousopo_victim_pdc_coef<-format(round(ols_leftDC_nousopo_victim_pdc$coefficients[2], 2))

ols_leftDC_nousopo_victim_pdc_lower_constant<-format(round(quantile(ols_leftDC_nousopo_victim_pdc_results$V1, .05), 2))
ols_leftDC_nousopo_victim_pdc_upper_constant<-format(round(quantile(ols_leftDC_nousopo_victim_pdc_results$V1, .95), 2))
ols_leftDC_nousopo_victim_pdc_coef_constant<-format(round(ols_leftDC_nousopo_victim_pdc$coefficients[1], 2))

##write(ols_leftDC_nousopo_victim_pdc_coef, 'ols_leftDC_nousopo_victim_pdc_coef.tex')
##write(ols_leftDC_nousopo_victim_pdc_lower, 'ols_leftDC_nousopo_victim_pdc_lower.tex')
##write(ols_leftDC_nousopo_victim_pdc_upper, 'ols_leftDC_nousopo_victim_pdc_upper.tex')

##write(ols_leftDC_nousopo_victim_pdc_coef_constant, 'ols_leftDC_nousopo_victim_pdc_coef_constant.tex')
##write(ols_leftDC_nousopo_victim_pdc_lower_constant, 'ols_leftDC_nousopo_victim_pdc_lower_constant.tex')
##write(ols_leftDC_nousopo_victim_pdc_upper_constant, 'ols_leftDC_nousopo_victim_pdc_upper_constant.tex')


######################################################################
#####Table B9: Close elections difference in means####################
######################################################################

#all candidates
dips73_close_elected<-subset(dips73, elected==1)
dips73_close_elected<-subset(dips73_close_elected, quotient_diff<=quantile(quotient_diff, .5))
dips73_close_unelected<-subset(dips73, elected==0)
dips73_close_unelected<-subset(dips73_close_unelected, quotient_diff>quantile(quotient_diff, .5))
dips73_close<-rbind(dips73_close_elected,dips73_close_unelected)

ttest_anyrepression_close<-ttest_electedvunelected(dips73_close$anyrepression, dips73_close)

##write(format(round(as.numeric(as.character(ttest_anyrepression_close$elected[1])), 2)), 'mean_anyrepression_close_elected.tex')
##write(format(round(as.numeric(as.character(ttest_anyrepression_close$unelected[1])), 2)), 'mean_anyrepression_close_unelected.tex')
##write(format(round(as.numeric(as.character(ttest_anyrepression_close$difference[1])), 2)), "mean_anyrepression_close_difference.tex")

dips73_close_elected_nousopo<-subset(dips73_nousopo, elected==1)
dips73_close_elected_nousopo<-subset(dips73_close_elected_nousopo, quotient_diff<=quantile(quotient_diff, .5))
dips73_close_unelected_nousopo<-subset(dips73_nousopo, elected==0)
dips73_close_unelected_nousopo<-subset(dips73_close_unelected_nousopo, quotient_diff>quantile(quotient_diff, .5))
dips73_close_nousopo<-rbind(dips73_close_elected_nousopo,dips73_close_unelected_nousopo)

ttest_anyrepression_close_nousopo<-ttest_electedvunelected(dips73_close_nousopo$anyrepression, dips73_close_nousopo)

##write(format(round(as.numeric(as.character(ttest_anyrepression_close_nousopo$elected[1])), 2)), 'mean_anyrepression_close_elected_nousopo.tex')
##write(format(round(as.numeric(as.character(ttest_anyrepression_close_nousopo$unelected[1])), 2)), 'mean_anyrepression_close_unelected_nousopo.tex')
##write(format(round(as.numeric(as.character(ttest_anyrepression_close_nousopo$difference[1])), 2)), "mean_anyrepression_close_difference_nousopo.tex")

##any repression,left only
dips73_leftDC_close_elected<-subset(dips73_leftDC_nousopo, elected==1)
dips73_leftDC_close_elected<-subset(dips73_leftDC_close_elected, quotient_diff<=quantile(quotient_diff, .5))
dips73_leftDC_close_unelected<-subset(dips73_leftDC_nousopo, elected==0)
dips73_leftDC_close_unelected<-subset(dips73_leftDC_close_unelected, quotient_diff>quantile(quotient_diff, .5))
dips73_leftDC_close<-rbind(dips73_leftDC_close_elected, dips73_leftDC_close_unelected)

ttest_anyrepression_leftDC_close<-ttest_electedvunelected(dips73_leftDC_close$anyrepression, dips73_leftDC_close)

##write(format(round(as.numeric(as.character(ttest_anyrepression_leftDC_close$elected[1])), 2)), 'mean_anyrepression_leftDC_close_elected_nousopo.tex')
##write(format(round(as.numeric(as.character(ttest_anyrepression_leftDC_close$unelected[1])), 2)), 'mean_anyrepression_leftDC_close_unelected_nousopo.tex')
##write(format(round(as.numeric(as.character(ttest_anyrepression_leftDC_close$difference[1])), 2)), "mean_anyrepression_leftDC_close_difference_nousopo.tex")


##physical coercion
ttest_physicalcoercion_close<-ttest_electedvunelected(dips73_close$victim, dips73_close)
##write(format(round(as.numeric(as.character(ttest_physicalcoercion_close$elected[1])), 2)), 'mean_physicalcoercion_close_elected.tex')
##write(format(round(as.numeric(as.character(ttest_physicalcoercion_close$unelected[1])), 2)), 'mean_physicalcoercion_close_unelected.tex')
##write(format(round(as.numeric(as.character(ttest_physicalcoercion_close$difference[1])), 2)), "mean_physicalcoercion_close_difference.tex")

##physical coercion
ttest_physicalcoercion_close_nousopo<-ttest_electedvunelected(dips73_close_nousopo$victim, dips73_close_nousopo)
##write(format(round(as.numeric(as.character(ttest_physicalcoercion_close_nousopo$elected[1])), 2)), 'mean_physicalcoercion_close_elected_nousopo.tex')
##write(format(round(as.numeric(as.character(ttest_physicalcoercion_close_nousopo$unelected[1])), 2)), 'mean_physicalcoercion_close_unelected_nousopo.tex')
##write(format(round(as.numeric(as.character(ttest_physicalcoercion_close_nousopo$difference[1])), 2)), "mean_physicalcoercion_close_difference_nousopo.tex")

#left only
ttest_leftDC_physicalcoercion_close<-ttest_electedvunelected(dips73_leftDC_close$victim, dips73_leftDC_close)

##write(format(round(as.numeric(as.character(ttest_leftDC_physicalcoercion_close$elected[1])), 2)), 'mean_physicalcoercion_close_leftDC_elected_nousopo.tex')
##write(format(round(as.numeric(as.character(ttest_leftDC_physicalcoercion_close$unelected[1])), 2)), 'mean_physicalcoercion_close_leftDC_unelected_nousopo.tex')
##write(format(round(as.numeric(as.character(ttest_leftDC_physicalcoercion_close$difference[1])), 2)), "mean_physicalcoercion_close_leftDC_difference_nousopo.tex")


#########################################################################
####Table B10: Testing for discontinuities in covariates#################
#########################################################################


late_all_surname<-format(round(RDestimate(surname~quotient_diff, data=dips73, cluster=dips73$Distrito)$est[1], 2))
##write(late_all_surname, "late_surname.tex")

late_all_gender<-format(round(RDestimate(gender~quotient_diff, data=dips73, cluster=dips73$Distrito)$est[1], 2))
##write(late_all_gender, "late_gender.tex")

late_all_dynastic<-format(round(RDestimate(dynastic~quotient_diff, data=dips73, cluster=dips73$Distrito)$est[1], 2))
##write(late_all_dynastic, "late_dynastic.tex")

late_all_incumbent<-format(round(RDestimate(incumbent_orpastelected~quotient_diff, data=dips73, cluster=dips73$Distrito)$est[1], 2))
##write(late_all_incumbent, "late_incumbent.tex")

late_all_list<-format(round(RDestimate(UP~quotient_diff, data=dips73, cluster=dips73$Distrito)$est[1], 2))
##write(late_all_list, "late_list.tex")

late_all_USOPO<-format(round(RDestimate(I(list=='USOPO')~quotient_diff, data=dips73, cluster=dips73$Distrito)$est[1], 2))
##write(late_all_USOPO, "late_USOPO.tex")

##write(format(round(RDestimate(surname~quotient_diff, data=dips73, cluster=dips73$Distrito)$obs[1], 2)), "obs_late_surname.tex")

##write(format(round(RDestimate(gender~quotient_diff, data=dips73, cluster=dips73$Distrito)$obs[1], 2)), "obs_late_gender.tex")

##write(format(round(RDestimate(dynastic~quotient_diff, data=dips73, cluster=dips73$Distrito)$obs[1], 2)), "obs_late_dynastic.tex")

##write(format(round(RDestimate(incumbent_orpastelected~quotient_diff, data=dips73, cluster=dips73$Distrito)$obs[1], 2)), "obs_late_incumbent.tex")

##write(format(round(RDestimate(UP~quotient_diff, data=dips73, cluster=dips73$Distrito)$obs[1], 2)), "obs_late_list.tex")

##write(format(round(RDestimate(I(list=='USOPO')~quotient_diff, data=dips73, cluster=dips73$Distrito)$obs[1], 2)), "obs_late_usopo.tex")

##write(format(round(RDestimate(surname~quotient_diff, data=dips73, cluster=dips73$Distrito)$bw[1], 2)), "bw_late_surname.tex")

##write(format(round(RDestimate(gender~quotient_diff, data=dips73, cluster=dips73$Distrito)$bw[1], 2)), "bw_late_gender.tex")

##write(format(round(RDestimate(dynastic~quotient_diff, data=dips73, cluster=dips73$Distrito)$bw[1], 2)), "bw_late_dynastic.tex")

##write(format(round(RDestimate(incumbent_orpastelected~quotient_diff, data=dips73, cluster=dips73$Distrito)$bw[1], 2)), "bw_late_incumbent.tex")

##write(format(round(RDestimate(UP~quotient_diff, data=dips73, cluster=dips73$Distrito)$bw[1], 2)), "bw_late_list.tex")

##write(format(round(RDestimate(I(list=='USOPO')~quotient_diff, data=dips73, cluster=dips73$Distrito)$bw[1], 2)), "bw_late_usopo.tex")

surname_results_cb=covariateBalance(surname_data)
surname_lower_cb<-format(round(quantile(surname_results_cb, .05), 2))
surname_upper_cb<-format(round(quantile(surname_results_cb, .95),2))
##write(surname_lower_cb, "surname_lower_cb.tex")
##write(surname_upper_cb, "surname_upper_cb.tex")

gender_results_cb=covariateBalance(gender_data)
gender_lower_cb<-format(round(quantile(gender_results_cb, .05), 2))
gender_upper_cb<-format(round(quantile(gender_results_cb, .95),2))
##write(gender_lower_cb, "gender_lower_cb.tex")
##write(gender_upper_cb, "gender_upper_cb.tex")

dynastic_results_cb=covariateBalance(dynastic_data)
dynastic_lower_cb<-format(round(quantile(dynastic_results_cb, .05), 2))
dynastic_upper_cb<-format(round(quantile(dynastic_results_cb, .95),2))
##write(dynastic_lower_cb, "dynastic_lower_cb.tex")
##write(dynastic_upper_cb, "dynastic_upper_cb.tex")

incumbent_results_cb=covariateBalance(incumbent_data)
incumbent_lower_cb<-format(round(quantile(incumbent_results_cb, .05), 2))
incumbent_upper_cb<-format(round(quantile(incumbent_results_cb, .95),2))
##write(incumbent_lower_cb, "incumbent_lower_cb.tex")
##write(incumbent_upper_cb, "incumbent_upper_cb.tex")

list_results_cb=covariateBalance(list_data)
list_lower_cb<-format(round(quantile(list_results_cb, .05), 2))
list_upper_cb<-format(round(quantile(list_results_cb, .95),2))
##write(list_lower_cb, "list_lower_cb.tex")
##write(list_upper_cb, "list_upper_cb.tex")

usopo_results_cb=covariateBalance(usopo_data)
usopo_lower_cb<-format(round(quantile(usopo_results_cb, .04), 2))
usopo_upper_cb<-format(round(quantile(usopo_results_cb, .95),2))
##write(usopo_lower_cb, "usopo_lower_cb.tex")
##write(usopo_upper_cb, "usopo_upper_cb.tex")



##############################################################################
###############Table B11: Excluding candidates near threshold################
##############################################################################

dips73_donut<-subset(dips73, abs(quotient_diff)>.25)

donut_rdd_anyrepression<-RDestimate(anyrepression~quotient_diff, data=dips73_donut, cluster=dips73_donut$Distrito)
donut_rdd_physicalcoercion<-RDestimate(victim~quotient_diff, data=dips73_donut, cluster=dips73_donut$Distrito)
donut_rdd_anyrepression_party<-RDestimate(anyrepression~quotient_diff|Partido, data=dips73_donut, cluster=dips73_donut$Distrito)
donut_rdd_physicalcoercion_party<-RDestimate(victim~quotient_diff|Partido, data=dips73_donut, cluster=dips73_donut$Distrito)

donut_LLR_anyrepression<-makeLLR(dips73_donut, dips73_donut$anyrepression, dips73_donut$quotient_diff, 0, 'dips73_donut')
donut_LLR_physicalcoercion<-makeLLR(dips73_donut, dips73_donut$victim, dips73_donut$quotient_diff, 0, 'dips73_donut')

donut_LLR_anyrepression_bootstrap<-bootstrapLLR(donut_LLR_anyrepression, iterations)
donut_LLR_physicalcoercion_bootstrap<-bootstrapLLR(donut_LLR_physicalcoercion, iterations)

##write(format(round(donut_rdd_anyrepression$est[1], 2)), "late_anyrepression_nousopo_donut.tex")
##write(format(round(donut_rdd_anyrepression$est[2], 2)), "half_bw_anyrepression_nousopo_donut.tex")
##write(format(round(donut_rdd_anyrepression$est[3], 2)), "double_bw_anyrepression_nousopo_donut.tex")

##write(format(round(donut_rdd_anyrepression_party$est[1], 2)), "latep_anyrepression_nousopo_donut.tex")
##write(format(round(donut_rdd_anyrepression_party$est[2], 2)), "halfp_anyrepression_nousopo_donut.tex")
##write(format(round(donut_rdd_anyrepression_party$est[3], 2)), "doublep_anyrepression_nousopo_donut.tex")

##write(donut_rdd_anyrepression$obs[1], "obs_late_anyrepression_nousopo_donut.tex")
##write(donut_rdd_anyrepression$obs[2], "obs_half_late_anyrepression_nousopo_donut.tex")
##write(donut_rdd_anyrepression$obs[3], "obs_double_late_anyrepression_nousopo_donut.tex")

##write(format(round(donut_rdd_anyrepression$bw[1],2)), "bw_late_anyrepression_nousopo_donut.tex")
##write(format(round(donut_rdd_anyrepression$bw[2],2)), "bw_half_late_anyrepression_nousopo_donut.tex")
##write(format(round(donut_rdd_anyrepression$bw[3],2)), "bw_double_late_anyrepression_nousopo_donut.tex")

late_lower_anyrepression_nousopo_donut<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLR, probs=.05), 2))
late_upper_anyrepression_nousopo_donut<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLR, probs=.95), 2))
##write(late_lower_anyrepression_nousopo_donut, "late_lower_anyrepression_nousopo_donut.tex")
##write(late_upper_anyrepression_nousopo_donut, "late_upper_anyrepression_nousopo_donut.tex")

late_lower_anyrepression_nousopo_donut_95<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLR, probs=.025), 2))
late_upper_anyrepression_nousopo_donut_95<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLR, probs=.975), 2))
##write(late_lower_anyrepression_nousopo_donut_95, "late_lower_anyrepression_nousopo_donut_95.tex")
##write(late_upper_anyrepression_nousopo_donut_95, "late_upper_anyrepression_nousopo_donut_95.tex")

late_half_lower_anyrepression_nousopo_donut<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRhalf, probs=.05), 2))
late_half_upper_anyrepression_nousopo_donut<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRhalf, probs=.95), 2))
##write(late_half_lower_anyrepression_nousopo_donut, "late_half_lower_anyrepression_nousopo_donut.tex")
##write(late_half_upper_anyrepression_nousopo_donut, "late_half_upper_anyrepression_nousopo_donut.tex")

late_half_lower_anyrepression_nousopo_donut_95<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRhalf, probs=.025), 2))
late_half_upper_anyrepression_nousopo_donut_95<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRhalf, probs=.975), 2))
##write(late_half_lower_anyrepression_nousopo_donut_95, "late_half_lower_anyrepression_nousopo_donut_95.tex")
##write(late_half_upper_anyrepression_nousopo_donut_95, "late_half_upper_anyrepression_nousopo_donut_95.tex")

late_2_lower_anyrepression_nousopo_donut<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLR2, probs=.05), 2))
late_2_upper_anyrepression_nousopo_donut<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLR2, probs=.95), 2))
##write(late_2_lower_anyrepression_nousopo_donut, "late_2_lower_anyrepression_nousopo_donut.tex")
##write(late_2_upper_anyrepression_nousopo_donut, "late_2_upper_anyrepression_nousopo_donut.tex")

late_2_lower_anyrepression_nousopo_donut_95<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLR2, probs=.025), 2))
late_2_upper_anyrepression_nousopo_donut_95<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLR2, probs=.975), 2))
##write(late_2_lower_anyrepression_nousopo_donut_95, "late_2_lower_anyrepression_nousopo_donut_95.tex")
##write(late_2_upper_anyrepression_nousopo_donut_95, "late_2_upper_anyrepression_nousopo_donut_95.tex")

late_party_lower_anyrepression_nousopo_donut<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRparty, probs=.05), 2))
late_party_upper_anyrepression_nousopo_donut<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRparty, probs=.95), 2))
##write(late_party_lower_anyrepression_nousopo_donut, "late_party_lower_anyrepression_nousopo_donut.tex")
##write(late_party_upper_anyrepression_nousopo_donut, "late_party_upper_anyrepression_nousopo_donut.tex")

late_party_lower_anyrepression_nousopo_donut_95<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRparty, probs=.025), 2))
late_party_upper_anyrepression_nousopo_donut_95<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRparty, probs=.975), 2))
##write(late_party_lower_anyrepression_nousopo_donut_95, "late_party_lower_anyrepression_nousopo_donut_95.tex")
##write(late_party_upper_anyrepression_nousopo_donut_95, "late_party_upper_anyrepression_nousopo_donut_95.tex")

late_party_half_lower_anyrepression_nousopo_donut<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRpartyhalf, probs=.05), 2))
late_party_half_upper_anyrepression_nousopo_donut<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRpartyhalf, probs=.95), 2))
##write(late_party_half_lower_anyrepression_nousopo_donut, "late_party_half_lower_anyrepression_nousopo_donut.tex")
##write(late_party_half_upper_anyrepression_nousopo_donut, "late_party_half_upper_anyrepression_nousopo_donut.tex")

late_party_half_lower_anyrepression_nousopo_donut_95<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRpartyhalf, probs=.025), 2))
late_party_half_upper_anyrepression_nousopo_donut_95<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRpartyhalf, probs=.975), 2))
##write(late_party_half_lower_anyrepression_nousopo_donut_95, "late_party_half_lower_anyrepression_nousopo_donut_95.tex")
##write(late_party_half_upper_anyrepression_nousopo_donut_95, "late_party_half_upper_anyrepression_nousopo_donut_95.tex")

late_party_2_lower_anyrepression_nousopo_donut<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRparty2, probs=.05), 2))
late_party_2_upper_anyrepression_nousopo_donut<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRparty2, probs=.95), 2))
##write(late_party_2_lower_anyrepression_nousopo_donut, "late_party_2_lower_anyrepression_nousopo_donut.tex")
##write(late_party_2_upper_anyrepression_nousopo_donut, "late_party_2_upper_anyrepression_nousopo_donut.tex")

late_party_2_lower_anyrepression_nousopo_donut_95<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRparty2, probs=.025), 2))
late_party_2_upper_anyrepression_nousopo_donut_95<-format(round(quantile(donut_LLR_anyrepression_bootstrap$LLRparty2, probs=.975), 2))
##write(late_party_2_lower_anyrepression_nousopo_donut_95, "late_party_2_lower_anyrepression_nousopo_donut_95.tex")
##write(late_party_2_upper_anyrepression_nousopo_donut_95, "late_party_2_upper_anyrepression_nousopo_donut_95.tex")

##write(format(round(donut_rdd_physicalcoercion$est[1], 2)), "late_physicalcoercion_nousopo_donut.tex")
##write(format(round(donut_rdd_physicalcoercion$est[2], 2)), "half_bw_physicalcoercion_nousopo_donut.tex")
##write(format(round(donut_rdd_physicalcoercion$est[3], 2)), "double_bw_physicalcoercion_nousopo_donut.tex")

##write(format(round(donut_rdd_physicalcoercion_party$est[1], 2)), "latep_physicalcoercion_nousopo_donut.tex")
##write(format(round(donut_rdd_physicalcoercion_party$est[2], 2)), "halfp_physicalcoercion_nousopo_donut.tex")
##write(format(round(donut_rdd_physicalcoercion_party$est[3], 2)), "doublep_physicalcoercion_nousopo_donut.tex")

##write(donut_rdd_physicalcoercion$obs[1], "obs_late_physicalcoercion_nousopo_donut.tex")
##write(donut_rdd_physicalcoercion$obs[2], "obs_half_late_physicalcoercion_nousopo_donut.tex")
##write(donut_rdd_physicalcoercion$obs[3], "obs_double_late_physicalcoercion_nousopo_donut.tex")

##write(format(round(donut_rdd_physicalcoercion$bw[1],2)), "bw_late_physicalcoercion_nousopo_donut.tex")
##write(format(round(donut_rdd_physicalcoercion$bw[2],2)), "bw_half_late_physicalcoercion_nousopo_donut.tex")
##write(format(round(donut_rdd_physicalcoercion$bw[3],2)), "bw_double_late_physicalcoercion_nousopo_donut.tex")

late_lower_physicalcoercion_nousopo_donut<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLR, probs=.05), 2))
late_upper_physicalcoercion_nousopo_donut<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLR, probs=.95), 2))
##write(late_lower_physicalcoercion_nousopo_donut, "late_lower_physicalcoercion_nousopo_donut.tex")
##write(late_upper_physicalcoercion_nousopo_donut, "late_upper_physicalcoercion_nousopo_donut.tex")

late_lower_physicalcoercion_nousopo_donut_95<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLR, probs=.025), 2))
late_upper_physicalcoercion_nousopo_donut_95<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLR, probs=.975), 2))
##write(late_lower_physicalcoercion_nousopo_donut_95, "late_lower_physicalcoercion_nousopo_donut_95.tex")
##write(late_upper_physicalcoercion_nousopo_donut_95, "late_upper_physicalcoercion_nousopo_donut_95.tex")

late_half_lower_physicalcoercion_nousopo_donut<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRhalf, probs=.05), 2))
late_half_upper_physicalcoercion_nousopo_donut<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRhalf, probs=.95), 2))
##write(late_half_lower_physicalcoercion_nousopo_donut, "late_half_lower_physicalcoercion_nousopo_donut.tex")
##write(late_half_upper_physicalcoercion_nousopo_donut, "late_half_upper_physicalcoercion_nousopo_donut.tex")

late_half_lower_physicalcoercion_nousopo_donut_95<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRhalf, probs=.025), 2))
late_half_upper_physicalcoercion_nousopo_donut_95<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRhalf, probs=.975), 2))
##write(late_half_lower_physicalcoercion_nousopo_donut_95, "late_half_lower_physicalcoercion_nousopo_donut_95.tex")
##write(late_half_upper_physicalcoercion_nousopo_donut_95, "late_half_upper_physicalcoercion_nousopo_donut_95.tex")

late_2_lower_physicalcoercion_nousopo_donut<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLR2, probs=.05), 2))
late_2_upper_physicalcoercion_nousopo_donut<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLR2, probs=.95), 2))
##write(late_2_lower_physicalcoercion_nousopo_donut, "late_2_lower_physicalcoercion_nousopo_donut.tex")
##write(late_2_upper_physicalcoercion_nousopo_donut, "late_2_upper_physicalcoercion_nousopo_donut.tex")

late_2_lower_physicalcoercion_nousopo_donut_95<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLR2, probs=.025), 2))
late_2_upper_physicalcoercion_nousopo_donut_95<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLR2, probs=.975), 2))
##write(late_2_lower_physicalcoercion_nousopo_donut_95, "late_2_lower_physicalcoercion_nousopo_donut_95.tex")
##write(late_2_upper_physicalcoercion_nousopo_donut_95, "late_2_upper_physicalcoercion_nousopo_donut_95.tex")

late_party_lower_physicalcoercion_nousopo_donut<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRparty, probs=.05), 2))
late_party_upper_physicalcoercion_nousopo_donut<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRparty, probs=.95), 2))
##write(late_party_lower_physicalcoercion_nousopo_donut, "late_party_lower_physicalcoercion_nousopo_donut.tex")
##write(late_party_upper_physicalcoercion_nousopo_donut, "late_party_upper_physicalcoercion_nousopo_donut.tex")

late_party_lower_physicalcoercion_nousopo_donut_95<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRparty, probs=.025), 2))
late_party_upper_physicalcoercion_nousopo_donut_95<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRparty, probs=.975), 2))
##write(late_party_lower_physicalcoercion_nousopo_donut_95, "late_party_lower_physicalcoercion_nousopo_donut_95.tex")
##write(late_party_upper_physicalcoercion_nousopo_donut_95, "late_party_upper_physicalcoercion_nousopo_donut_95.tex")

late_party_half_lower_physicalcoercion_nousopo_donut<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRpartyhalf, probs=.05), 2))
late_party_half_upper_physicalcoercion_nousopo_donut<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRpartyhalf, probs=.95), 2))
##write(late_party_half_lower_physicalcoercion_nousopo_donut, "late_party_half_lower_physicalcoercion_nousopo_donut.tex")
##write(late_party_half_upper_physicalcoercion_nousopo_donut, "late_party_half_upper_physicalcoercion_nousopo_donut.tex")

late_party_half_lower_physicalcoercion_nousopo_donut_95<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRpartyhalf, probs=.025), 2))
late_party_half_upper_physicalcoercion_nousopo_donut_95<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRpartyhalf, probs=.975), 2))
##write(late_party_half_lower_physicalcoercion_nousopo_donut_95, "late_party_half_lower_physicalcoercion_nousopo_donut_95.tex")
##write(late_party_half_upper_physicalcoercion_nousopo_donut_95, "late_party_half_upper_physicalcoercion_nousopo_donut_95.tex")

late_party_2_lower_physicalcoercion_nousopo_donut<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRparty2, probs=.05), 2))
late_party_2_upper_physicalcoercion_nousopo_donut<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRparty2, probs=.95), 2))
##write(late_party_2_lower_physicalcoercion_nousopo_donut, "late_party_2_lower_physicalcoercion_nousopo_donut.tex")
##write(late_party_2_upper_physicalcoercion_nousopo_donut, "late_party_2_upper_physicalcoercion_nousopo_donut.tex")

late_party_2_lower_physicalcoercion_nousopo_donut_95<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRparty2, probs=.025), 2))
late_party_2_upper_physicalcoercion_nousopo_donut_95<-format(round(quantile(donut_LLR_physicalcoercion_bootstrap$LLRparty2, probs=.975), 2))
##write(late_party_2_lower_physicalcoercion_nousopo_donut_95, "late_party_2_lower_physicalcoercion_nousopo_donut_95.tex")
##write(late_party_2_upper_physicalcoercion_nousopo_donut_95, "late_party_2_upper_physicalcoercion_nousopo_donut_95.tex")


#######################################################################
####################Figure B2: Alternative Bandwidths##################
#######################################################################

alt_df<-data.frame(cbind(NA, NA, NA, NA, NA))
colnames(alt_df)<-c('est', 'lower', 'upper', 'bw', 'CI')

for (i in seq(1, 5, by=0.25)){
  rd_est<-RDestimate(victim~quotient_diff, data=dips73, cluster=dips73$Distrito, bw=i)
  est<-rd_est$est[1]
  
  alt_data<-makeAlt(dips73, dips73$victim, dips73$quotient_diff, 0, 'dips73', i)
  
  alt_results<-bootstrapLLR(alt_data, iterations)
  
  lower_90<-quantile(alt_results$LLR, probs=.05)
  upper_90<-quantile(alt_results$LLR, probs=.95)
  
  lower_95<-quantile(alt_results$LLR, probs=.025)
  upper_95<-quantile(alt_results$LLR, probs=.975)
  
  alt_df_90<-data.frame(cbind(est, lower_90, upper_90, i, '90%'))
  alt_df_95<-data.frame(cbind(est, lower_95, upper_95, i, '95%'))
  colnames(alt_df_90)<-c('est', 'lower', 'upper', 'bw', 'CI')
  colnames(alt_df_95)<-c('est', 'lower', 'upper', 'bw', 'CI')
  alt_df_new<-rbind(alt_df_90, alt_df_95)
  
  alt_df<-rbind(alt_df, alt_df_new)
}

alt_df<-alt_df[-1,]

alt_plot<-ggplot(alt_df, aes(x=as.numeric(as.character(bw)), y=as.numeric(as.character(est)),
                             linetype=CI, ymin=as.numeric(as.character(lower)), 
                             ymax=as.numeric(as.character(upper))))+
  geom_pointrange(size=.5)+scale_linetype_manual(values=c('solid', 'dotted'),
                                                 guide = guide_legend(override.aes = list(
                                                   linetype = c("solid", "dotted"),
                                                   shape=c(NA, NA)) ))+theme_bw()+
  theme(legend.position="bottom",  axis.title=element_text(size=16),
        text=element_text(family="Times New Roman", size=16),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+geom_hline(aes(yintercept=0), lty=4) 

#ggsave('alt_plot.pdf', plot=alt_plot, height=4, width=6)


#######################################################################
####################Table B12: Naive Linear Regression#################
#######################################################################

trim3<-subset(dips73, abs(quotient_diff)<3)
ols_trim3<-linearRegressionFun(dips73, 3, 'dips73')

elected_ols_trim3_anyrepression_reg<-lm(anyrepression~factor(elected)*quotient_diff, data=trim3)
elected_ols_trim3_anyrepression<-format(round(summary(elected_ols_trim3_anyrepression_reg)$coefficients[2,1], 2))
elected_ols_trim3_obs<-format(dim(trim3)[1])
elected_ols_trim3_anyrepression_lower<-format(round(quantile(ols_trim3$ols_anyrepression, .05), 2))
elected_ols_trim3_anyrepression_upper<-format(round(quantile(ols_trim3$ols_anyrepression, .95), 2))
elected_ols_trim3_anyrepression_lower_95<-format(round(quantile(ols_trim3$ols_anyrepression, .025), 2))
elected_ols_trim3_anyrepression_upper_95<-format(round(quantile(ols_trim3$ols_anyrepression, .975), 2))

##write(elected_ols_trim3_anyrepression, "elected_ols_trim3_anyrepression.tex")
##write(elected_ols_trim3_obs, "elected_ols_trim3_obs.tex")
##write(elected_ols_trim3_anyrepression_lower, "elected_ols_trim3_anyrepression_lower.tex")
##write(elected_ols_trim3_anyrepression_upper, "elected_ols_trim3_anyrepression_upper.tex")
##write(elected_ols_trim3_anyrepression_lower_95, "elected_ols_trim3_anyrepression_lower_95.tex")
##write(elected_ols_trim3_anyrepression_upper_95, "elected_ols_trim3_anyrepression_upper_95.tex")

elected_ols_trim3_physicalcoercion_reg<-lm(victim~factor(elected)*quotient_diff, data=trim3)
elected_ols_trim3_physicalcoercion<-format(round(summary(elected_ols_trim3_physicalcoercion_reg)$coefficients[2,1], 2))
elected_ols_trim3_physicalcoercion_lower<-format(round(quantile(ols_trim3$ols_physicalcoercion, .05), 2))
elected_ols_trim3_physicalcoercion_upper<-format(round(quantile(ols_trim3$ols_physicalcoercion, .95), 2))

elected_ols_trim3_physicalcoercion_lower_95<-format(round(quantile(ols_trim3$ols_physicalcoercion, .025), 2))
elected_ols_trim3_physicalcoercion_upper_95<-format(round(quantile(ols_trim3$ols_physicalcoercion, .975), 2))

##write(elected_ols_trim3_physicalcoercion, "elected_ols_trim3_physicalcoercion.tex")
##write(elected_ols_trim3_physicalcoercion_lower, "elected_ols_trim3_physicalcoercion_lower.tex")
##write(elected_ols_trim3_physicalcoercion_upper, "elected_ols_trim3_physicalcoercion_upper.tex")
##write(elected_ols_trim3_physicalcoercion_lower_95, "elected_ols_trim3_physicalcoercion_lower_95.tex")
##write(elected_ols_trim3_physicalcoercion_upper_95, "elected_ols_trim3_physicalcoercion_upper_95.tex")

elected_ols_trim3p_anyrepression_reg<-lm(anyrepression~factor(elected)*quotient_diff+Partido, data=trim3)
elected_ols_trim3p_anyrepression<-format(round(summary(elected_ols_trim3p_anyrepression_reg)$coefficients[2,1], 2))
elected_ols_trim3p_anyrepression_lower<-format(round(quantile(ols_trim3$ols_anyrepression_party, .05), 2))
elected_ols_trim3p_anyrepression_upper<-format(round(quantile(ols_trim3$ols_anyrepression_party, .95), 2))
elected_ols_trim3p_anyrepression_lower_95<-format(round(quantile(ols_trim3$ols_anyrepression_party, .025), 2))
elected_ols_trim3p_anyrepression_upper_95<-format(round(quantile(ols_trim3$ols_anyrepression_party, .975), 2))

##write(elected_ols_trim3p_anyrepression, "elected_ols_trim3p_anyrepression.tex")
##write(elected_ols_trim3p_anyrepression_lower, "elected_ols_trim3p_anyrepression_lower.tex")
##write(elected_ols_trim3p_anyrepression_upper, "elected_ols_trim3p_anyrepression_upper.tex")
##write(elected_ols_trim3p_anyrepression_lower_95, "elected_ols_trim3p_anyrepression_lower_95.tex")
##write(elected_ols_trim3p_anyrepression_upper_95, "elected_ols_trim3p_anyrepression_upper_95.tex")

elected_ols_trim3p_physicalcoercion_reg<-lm(victim~factor(elected)*quotient_diff+Partido, data=trim3)
elected_ols_trim3p_physicalcoercion<-format(round(summary(elected_ols_trim3p_physicalcoercion_reg)$coefficients[2,1], 2))
elected_ols_trim3p_physicalcoercion_lower<-format(round(quantile(ols_trim3$ols_physicalcoercion_party, .05), 2))
elected_ols_trim3p_physicalcoercion_upper<-format(round(quantile(ols_trim3$ols_physicalcoercion_party, .95), 2))
elected_ols_trim3p_physicalcoercion_lower_95<-format(round(quantile(ols_trim3$ols_physicalcoercion_party, .025), 2))
elected_ols_trim3p_physicalcoercion_upper_95<-format(round(quantile(ols_trim3$ols_physicalcoercion_party, .975), 2))

##write(elected_ols_trim3p_physicalcoercion, "elected_ols_trim3p_physicalcoercion.tex")
##write(elected_ols_trim3p_physicalcoercion_lower, "elected_ols_trim3p_physicalcoercion_lower.tex")
##write(elected_ols_trim3p_physicalcoercion_upper, "elected_ols_trim3p_physicalcoercion_upper.tex")
##write(elected_ols_trim3p_physicalcoercion_lower_95, "elected_ols_trim3p_physicalcoercion_lower_95.tex")
##write(elected_ols_trim3p_physicalcoercion_upper_95, "elected_ols_trim3p_physicalcoercion_upper_95.tex")


##trim 2

trim2<-subset(dips73, abs(quotient_diff)<2)
ols_trim2<-linearRegressionFun(dips73, 2, 'dips73')

elected_ols_trim2_anyrepression_reg<-lm(anyrepression~factor(elected)*quotient_diff, data=trim2)
elected_ols_trim2_anyrepression<-format(round(summary(elected_ols_trim2_anyrepression_reg)$coefficients[2,1], 2))
elected_ols_trim2_obs<-format(dim(trim2)[1])
elected_ols_trim2_anyrepression_lower<-format(round(quantile(ols_trim2$ols_anyrepression, .05), 2))
elected_ols_trim2_anyrepression_upper<-format(round(quantile(ols_trim2$ols_anyrepression, .95), 2))
elected_ols_trim2_anyrepression_lower_95<-format(round(quantile(ols_trim2$ols_anyrepression, .025), 2))
elected_ols_trim2_anyrepression_upper_95<-format(round(quantile(ols_trim2$ols_anyrepression, .975), 2))

##write(elected_ols_trim2_anyrepression, "elected_ols_trim2_anyrepression.tex")
##write(elected_ols_trim2_obs, "elected_ols_trim2_obs.tex")
##write(elected_ols_trim2_anyrepression_lower, "elected_ols_trim2_anyrepression_lower.tex")
##write(elected_ols_trim2_anyrepression_upper, "elected_ols_trim2_anyrepression_upper.tex")
##write(elected_ols_trim2_anyrepression_lower_95, "elected_ols_trim2_anyrepression_lower_95.tex")
##write(elected_ols_trim2_anyrepression_upper_95, "elected_ols_trim2_anyrepression_upper_95.tex")

elected_ols_trim2_physicalcoercion_reg<-lm(victim~factor(elected)*quotient_diff, data=trim2)
elected_ols_trim2_physicalcoercion<-format(round(summary(elected_ols_trim2_physicalcoercion_reg)$coefficients[2,1], 2))
elected_ols_trim2_physicalcoercion_lower<-format(round(quantile(ols_trim2$ols_physicalcoercion, .05), 2))
elected_ols_trim2_physicalcoercion_upper<-format(round(quantile(ols_trim2$ols_physicalcoercion, .95), 2))

elected_ols_trim2_physicalcoercion_lower_95<-format(round(quantile(ols_trim2$ols_physicalcoercion, .025), 2))
elected_ols_trim2_physicalcoercion_upper_95<-format(round(quantile(ols_trim2$ols_physicalcoercion, .975), 2))

##write(elected_ols_trim2_physicalcoercion, "elected_ols_trim2_physicalcoercion.tex")
##write(elected_ols_trim2_physicalcoercion_lower, "elected_ols_trim2_physicalcoercion_lower.tex")
##write(elected_ols_trim2_physicalcoercion_upper, "elected_ols_trim2_physicalcoercion_upper.tex")
##write(elected_ols_trim2_physicalcoercion_lower_95, "elected_ols_trim2_physicalcoercion_lower_95.tex")
##write(elected_ols_trim2_physicalcoercion_upper_95, "elected_ols_trim2_physicalcoercion_upper_95.tex")

elected_ols_trim2p_anyrepression_reg<-lm(anyrepression~factor(elected)*quotient_diff+Partido, data=trim2)
elected_ols_trim2p_anyrepression<-format(round(summary(elected_ols_trim2p_anyrepression_reg)$coefficients[2,1], 2))
elected_ols_trim2p_anyrepression_lower<-format(round(quantile(ols_trim2$ols_anyrepression_party, .05), 2))
elected_ols_trim2p_anyrepression_upper<-format(round(quantile(ols_trim2$ols_anyrepression_party, .95), 2))
elected_ols_trim2p_anyrepression_lower_95<-format(round(quantile(ols_trim2$ols_anyrepression_party, .025), 2))
elected_ols_trim2p_anyrepression_upper_95<-format(round(quantile(ols_trim2$ols_anyrepression_party, .975), 2))

##write(elected_ols_trim2p_anyrepression, "elected_ols_trim2p_anyrepression.tex")
##write(elected_ols_trim2p_anyrepression_lower, "elected_ols_trim2p_anyrepression_lower.tex")
##write(elected_ols_trim2p_anyrepression_upper, "elected_ols_trim2p_anyrepression_upper.tex")
##write(elected_ols_trim2p_anyrepression_lower_95, "elected_ols_trim2p_anyrepression_lower_95.tex")
##write(elected_ols_trim2p_anyrepression_upper_95, "elected_ols_trim2p_anyrepression_upper_95.tex")

elected_ols_trim2p_physicalcoercion_reg<-lm(victim~factor(elected)*quotient_diff+Partido, data=trim2)
elected_ols_trim2p_physicalcoercion<-format(round(summary(elected_ols_trim2p_physicalcoercion_reg)$coefficients[2,1], 2))
elected_ols_trim2p_physicalcoercion_lower<-format(round(quantile(ols_trim2$ols_physicalcoercion_party, .05), 2))
elected_ols_trim2p_physicalcoercion_upper<-format(round(quantile(ols_trim2$ols_physicalcoercion_party, .95), 2))
elected_ols_trim2p_physicalcoercion_lower_95<-format(round(quantile(ols_trim2$ols_physicalcoercion_party, .025), 2))
elected_ols_trim2p_physicalcoercion_upper_95<-format(round(quantile(ols_trim2$ols_physicalcoercion_party, .975), 2))

##write(elected_ols_trim2p_physicalcoercion, "elected_ols_trim2p_physicalcoercion.tex")
##write(elected_ols_trim2p_physicalcoercion_lower, "elected_ols_trim2p_physicalcoercion_lower.tex")
##write(elected_ols_trim2p_physicalcoercion_upper, "elected_ols_trim2p_physicalcoercion_upper.tex")
##write(elected_ols_trim2p_physicalcoercion_lower_95, "elected_ols_trim2p_physicalcoercion_lower_95.tex")
##write(elected_ols_trim2p_physicalcoercion_upper_95, "elected_ols_trim2p_physicalcoercion_upper_95.tex")


##trim 6
trim6<-subset(dips73, abs(quotient_diff)<6)
ols_trim6<-linearRegressionFun(dips73, 6, 'dips73')

elected_ols_trim6_anyrepression_reg<-lm(anyrepression~factor(elected)*quotient_diff, data=trim6)
elected_ols_trim6_anyrepression<-format(round(summary(elected_ols_trim6_anyrepression_reg)$coefficients[2,1], 2))
elected_ols_trim6_obs<-format(dim(trim6)[1])
elected_ols_trim6_anyrepression_lower<-format(round(quantile(ols_trim6$ols_anyrepression, .05), 2))
elected_ols_trim6_anyrepression_upper<-format(round(quantile(ols_trim6$ols_anyrepression, .95), 2))
elected_ols_trim6_anyrepression_lower_95<-format(round(quantile(ols_trim6$ols_anyrepression, .025), 2))
elected_ols_trim6_anyrepression_upper_95<-format(round(quantile(ols_trim6$ols_anyrepression, .975), 2))

##write(elected_ols_trim6_anyrepression, "elected_ols_trim6_anyrepression.tex")
##write(elected_ols_trim6_obs, "elected_ols_trim6_obs.tex")
##write(elected_ols_trim6_anyrepression_lower, "elected_ols_trim6_anyrepression_lower.tex")
##write(elected_ols_trim6_anyrepression_upper, "elected_ols_trim6_anyrepression_upper.tex")
##write(elected_ols_trim6_anyrepression_lower_95, "elected_ols_trim6_anyrepression_lower_95.tex")
##write(elected_ols_trim6_anyrepression_upper_95, "elected_ols_trim6_anyrepression_upper_95.tex")

elected_ols_trim6_physicalcoercion_reg<-lm(victim~factor(elected)*quotient_diff, data=trim6)
elected_ols_trim6_physicalcoercion<-format(round(summary(elected_ols_trim6_physicalcoercion_reg)$coefficients[2,1], 2))
elected_ols_trim6_physicalcoercion_lower<-format(round(quantile(ols_trim6$ols_physicalcoercion, .05), 2))
elected_ols_trim6_physicalcoercion_upper<-format(round(quantile(ols_trim6$ols_physicalcoercion, .95), 2))

elected_ols_trim6_physicalcoercion_lower_95<-format(round(quantile(ols_trim6$ols_physicalcoercion, .025), 2))
elected_ols_trim6_physicalcoercion_upper_95<-format(round(quantile(ols_trim6$ols_physicalcoercion, .975), 2))

##write(elected_ols_trim6_physicalcoercion, "elected_ols_trim6_physicalcoercion.tex")
##write(elected_ols_trim6_physicalcoercion_lower, "elected_ols_trim6_physicalcoercion_lower.tex")
##write(elected_ols_trim6_physicalcoercion_upper, "elected_ols_trim6_physicalcoercion_upper.tex")
##write(elected_ols_trim6_physicalcoercion_lower_95, "elected_ols_trim6_physicalcoercion_lower_95.tex")
##write(elected_ols_trim6_physicalcoercion_upper_95, "elected_ols_trim6_physicalcoercion_upper_95.tex")

elected_ols_trim6p_anyrepression_reg<-lm(anyrepression~factor(elected)*quotient_diff+Partido, data=trim6)
elected_ols_trim6p_anyrepression<-format(round(summary(elected_ols_trim6p_anyrepression_reg)$coefficients[2,1], 2))
elected_ols_trim6p_anyrepression_lower<-format(round(quantile(ols_trim6$ols_anyrepression_party, .05), 2))
elected_ols_trim6p_anyrepression_upper<-format(round(quantile(ols_trim6$ols_anyrepression_party, .95), 2))
elected_ols_trim6p_anyrepression_lower_95<-format(round(quantile(ols_trim6$ols_anyrepression_party, .025), 2))
elected_ols_trim6p_anyrepression_upper_95<-format(round(quantile(ols_trim6$ols_anyrepression_party, .975), 2))

##write(elected_ols_trim6p_anyrepression, "elected_ols_trim6p_anyrepression.tex")
##write(elected_ols_trim6p_anyrepression_lower, "elected_ols_trim6p_anyrepression_lower.tex")
##write(elected_ols_trim6p_anyrepression_upper, "elected_ols_trim6p_anyrepression_upper.tex")
##write(elected_ols_trim6p_anyrepression_lower_95, "elected_ols_trim6p_anyrepression_lower_95.tex")
##write(elected_ols_trim6p_anyrepression_upper_95, "elected_ols_trim6p_anyrepression_upper_95.tex")

elected_ols_trim6p_physicalcoercion_reg<-lm(victim~factor(elected)*quotient_diff+Partido, data=trim6)
elected_ols_trim6p_physicalcoercion<-format(round(summary(elected_ols_trim6p_physicalcoercion_reg)$coefficients[2,1], 2))
elected_ols_trim6p_physicalcoercion_lower<-format(round(quantile(ols_trim6$ols_physicalcoercion_party, .05), 2))
elected_ols_trim6p_physicalcoercion_upper<-format(round(quantile(ols_trim6$ols_physicalcoercion_party, .95), 2))
elected_ols_trim6p_physicalcoercion_lower_95<-format(round(quantile(ols_trim6$ols_physicalcoercion_party, .025), 2))
elected_ols_trim6p_physicalcoercion_upper_95<-format(round(quantile(ols_trim6$ols_physicalcoercion_party, .975), 2))

##write(elected_ols_trim6p_physicalcoercion, "elected_ols_trim6p_physicalcoercion.tex")
##write(elected_ols_trim6p_physicalcoercion_lower, "elected_ols_trim6p_physicalcoercion_lower.tex")
##write(elected_ols_trim6p_physicalcoercion_upper, "elected_ols_trim6p_physicalcoercion_upper.tex")
##write(elected_ols_trim6p_physicalcoercion_lower_95, "elected_ols_trim6p_physicalcoercion_lower_95.tex")
##write(elected_ols_trim6p_physicalcoercion_upper_95, "elected_ols_trim6p_physicalcoercion_upper_95.tex")

###########################################################
###########Figure B3: Placebo Tests########################
###########################################################

#Given number of test points, function takes ~1 hour to run

placebo_df<-data.frame(cbind(NA, NA, NA, NA, NA))
colnames(placebo_df)<-c('est', 'lower', 'upper', 'cutpoint', 'CI')

for (i in seq(.5, 1.9, by=0.1)){
  rd_pos<-RDestimate(victim~quotient_diff, data=dips73, cluster=dips73$Distrito, cutpoint=i)
  rd_neg<-RDestimate(victim~quotient_diff, data=dips73, cluster=dips73$Distrito, cutpoint=i*(-1))
  est_pos<-rd_pos$est[1]
  est_neg<-rd_neg$est[1]
  
  placebo_LLR_data_pos<-makeLLR(dips73, dips73$victim, dips73$quotient_diff, i, 'dips73')
  placebo_LLR_data_neg<-makeLLR(dips73, dips73$victim, dips73$quotient_diff, i*(-1), 'dips73')
  
  placebo_bootstrap_pos<-bootstrapLLR(placebo_LLR_data_pos, iterations)
  placebo_bootstrap_neg<-bootstrapLLR(placebo_LLR_data_neg, iterations)
  
  lower_pos_90<-quantile(placebo_bootstrap_pos$LLR, probs=.05)
  upper_pos_90<-quantile(placebo_bootstrap_pos$LLR, probs=.95)
  lower_pos<-quantile(placebo_bootstrap_pos$LLR, probs=.025)
  upper_pos<-quantile(placebo_bootstrap_pos$LLR, probs=.975)
  
  lower_neg_90<-quantile(placebo_bootstrap_neg$LLR, probs=.05)
  upper_neg_90<-quantile(placebo_bootstrap_neg$LLR, probs=.95)
  lower_neg<-quantile(placebo_bootstrap_neg$LLR, probs=.025)
  upper_neg<-quantile(placebo_bootstrap_neg$LLR, probs=.975)
  
  placebo_df_pos<-data.frame(cbind(est_pos, lower_pos, upper_pos, i, '95%'))
  placebo_df_neg<-data.frame(cbind(est_neg, lower_neg, upper_neg, i*(-1), '95%'))
  placebo_df_pos_90<-data.frame(cbind(est_pos, lower_pos_90, upper_pos_90, i, '90%'))
  placebo_df_neg_90<-data.frame(cbind(est_neg, lower_neg_90, upper_neg_90, i*(-1), '90%'))
  
  colnames(placebo_df_pos)<-c('est', 'lower', 'upper', 'cutpoint', 'CI')
  colnames(placebo_df_neg)<-c('est', 'lower', 'upper', 'cutpoint', 'CI')
  colnames(placebo_df_pos_90)<-c('est', 'lower', 'upper', 'cutpoint', 'CI')
  colnames(placebo_df_neg_90)<-c('est', 'lower', 'upper', 'cutpoint', 'CI')
  
  placebo_df<-rbind(placebo_df, placebo_df_pos)
  placebo_df<-rbind(placebo_df, placebo_df_neg)
  placebo_df<-rbind(placebo_df, placebo_df_pos_90)
  placebo_df<-rbind(placebo_df, placebo_df_neg_90)
  
}

placebo_df<-placebo_df[-1,]
placebo_plot<-ggplot(placebo_df, aes(x=as.numeric(as.character(cutpoint)), y=as.numeric(as.character(est)), linetype=CI, ymin=as.numeric(as.character(lower)), ymax=as.numeric(as.character(upper))))+
  geom_pointrange(size=.5)+scale_linetype_manual(values=c('solid', 'dotted'),
                                                 guide = guide_legend(override.aes = list(
                                                   linetype = c("solid", "dotted"),
                                                   shape=c(NA, NA)) ))+theme_bw()+
  theme(legend.position="bottom",  axis.title=element_text(size=16),
        text=element_text(family="Times New Roman", size=16),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+geom_hline(aes(yintercept=0), lty=4) 

#ggsave('placebo_plot.pdf', plot=placebo_plot, height=4, width=6)


###############################################################
#######################Figure B4: Robustness checks############
###############################################################


##Minimum detectable effect is -.28 at 80% power
rdpower(dips73[c('victim', 'quotient_diff')], tau=-.28, all=TRUE)

robust_90<-rdrobust(dips73$victim, dips73$quotient_diff, level=90)
robust_90_est_con<-robust_90$coef[1]
robust_90_est_rob<-robust_90$coef[3]
robust_90_ci_con_lower<-robust_90$ci[1, 1]
robust_90_ci_con_upper<-robust_90$ci[1, 2]
robust_90_ci_rob_lower<-robust_90$ci[3, 1]
robust_90_ci_rob_upper<-robust_90$ci[3, 2]
robust_90_con_df<-data.frame(cbind(robust_90_est_con, robust_90_ci_con_lower, robust_90_ci_con_upper, '90%', 'conventional'))
colnames(robust_90_con_df)<-c('est', 'lower', 'upper', 'CI', 'type')
robust_90_rob_df<-data.frame(cbind(robust_90_est_rob, robust_90_ci_rob_lower, robust_90_ci_rob_upper, '90%', 'robust'))
colnames(robust_90_rob_df)<-c('est', 'lower', 'upper', 'CI', 'type')

robust_95<-rdrobust(dips73$victim, dips73$quotient_diff, level=95)
robust_95_est_con<-robust_95$coef[1]
robust_95_est_rob<-robust_95$coef[3]
robust_95_ci_con_lower<-robust_95$ci[1, 1]
robust_95_ci_con_upper<-robust_95$ci[1, 2]
robust_95_ci_rob_lower<-robust_95$ci[3, 1]
robust_95_ci_rob_upper<-robust_95$ci[3, 2]
robust_95_con_df<-data.frame(cbind(robust_95_est_con, robust_95_ci_con_lower, robust_95_ci_con_upper, '95%', 'conventional'))
colnames(robust_95_con_df)<-c('est', 'lower', 'upper', 'CI', 'type')
robust_95_rob_df<-data.frame(cbind(robust_95_est_rob, robust_95_ci_rob_lower, robust_95_ci_rob_upper, '95%', 'robust'))
colnames(robust_95_rob_df)<-c('est', 'lower', 'upper', 'CI', 'type')


##minimum detectable effect is -.19
rdpower(dips73[c('victim', 'quotient_diff')], tau=-.19, cluster=dips73$Distrito, all=TRUE)

robust_90_cluster<-rdrobust(dips73$victim, dips73$quotient_diff, level=90, cluster=dips73$Distrito)
robust_90_cluster_est_con<-robust_90_cluster$coef[1]
robust_90_cluster_est_rob<-robust_90_cluster$coef[3]
robust_90_cluster_ci_con_lower<-robust_90_cluster$ci[1, 1]
robust_90_cluster_ci_con_upper<-robust_90_cluster$ci[1, 2]
robust_90_cluster_ci_rob_lower<-robust_90_cluster$ci[3, 1]
robust_90_cluster_ci_rob_upper<-robust_90_cluster$ci[3, 2]
robust_90_cluster_con_df<-data.frame(cbind(robust_90_cluster_est_con, robust_90_cluster_ci_con_lower, robust_90_cluster_ci_con_upper, '90%', 'conventional_cluster'))
colnames(robust_90_cluster_con_df)<-c('est', 'lower', 'upper', 'CI', 'type')
robust_90_cluster_rob_df<-data.frame(cbind(robust_90_cluster_est_rob, robust_90_cluster_ci_rob_lower, robust_90_cluster_ci_rob_upper, '90%', 'robust_cluster'))
colnames(robust_90_cluster_rob_df)<-c('est', 'lower', 'upper', 'CI', 'type')

##minimum detectable effect is -.195 (rounded to -.2)
rdpower(dips73[c('victim', 'quotient_diff')], tau=-.195, cluster=dips73$Distrito, all=TRUE)

robust_95_cluster<-rdrobust(dips73$victim, dips73$quotient_diff, level=95, cluster=dips73$Distrito)
robust_95_cluster_est_con<-robust_95_cluster$coef[1]
robust_95_cluster_est_rob<-robust_95_cluster$coef[3]
robust_95_cluster_ci_con_lower<-robust_95_cluster$ci[1, 1]
robust_95_cluster_ci_con_upper<-robust_95_cluster$ci[1, 2]
robust_95_cluster_ci_rob_lower<-robust_95_cluster$ci[3, 1]
robust_95_cluster_ci_rob_upper<-robust_95_cluster$ci[3, 2]
robust_95_cluster_con_df<-data.frame(cbind(robust_95_cluster_est_con, robust_95_cluster_ci_con_lower, robust_95_cluster_ci_con_upper, '95%', 'conventional_cluster'))
colnames(robust_95_cluster_con_df)<-c('est', 'lower', 'upper', 'CI', 'type')
robust_95_cluster_rob_df<-data.frame(cbind(robust_95_cluster_est_rob, robust_95_cluster_ci_rob_lower, robust_95_cluster_ci_rob_upper, '95%', 'robust_cluster'))
colnames(robust_95_cluster_rob_df)<-c('est', 'lower', 'upper', 'CI', 'type')


#minimum detectable effect is -.24
rdpower(dips73[c('victim', 'quotient_diff')], tau=-.24, p=2, cluster=dips73$Distrito, all=TRUE)

robust_90_poly<-rdrobust(dips73$victim, dips73$quotient_diff, level=90, cluster=dips73$Distrito, p=2)
robust_90_poly_est_con<-robust_90_poly$coef[1]
robust_90_poly_est_rob<-robust_90_poly$coef[3]
robust_90_poly_ci_con_lower<-robust_90_poly$ci[1, 1]
robust_90_poly_ci_con_upper<-robust_90_poly$ci[1, 2]
robust_90_poly_ci_rob_lower<-robust_90_poly$ci[3, 1]
robust_90_poly_ci_rob_upper<-robust_90_poly$ci[3, 2]
robust_90_poly_con_df<-data.frame(cbind(robust_90_poly_est_con, robust_90_poly_ci_con_lower, robust_90_poly_ci_con_upper, '90%', 'conventional_poly'))
colnames(robust_90_poly_con_df)<-c('est', 'lower', 'upper', 'CI', 'type')
robust_90_poly_rob_df<-data.frame(cbind(robust_90_poly_est_rob, robust_90_poly_ci_rob_lower, robust_90_poly_ci_rob_upper, '90%', 'robust_poly'))
colnames(robust_90_poly_rob_df)<-c('est', 'lower', 'upper', 'CI', 'type')

#minimum detectable effect is -.28
rdpower(dips73[c('victim', 'quotient_diff')], tau=-.28, p=2, cluster=dips73$Distrito, all=TRUE)

robust_95_poly<-rdrobust(dips73$victim, dips73$quotient_diff, level=95, cluster=dips73$Distrito, p=2)
robust_95_poly_est_con<-robust_95_poly$coef[1]
robust_95_poly_est_rob<-robust_95_poly$coef[3]
robust_95_poly_ci_con_lower<-robust_95_poly$ci[1, 1]
robust_95_poly_ci_con_upper<-robust_95_poly$ci[1, 2]
robust_95_poly_ci_rob_lower<-robust_95_poly$ci[3, 1]
robust_95_poly_ci_rob_upper<-robust_95_poly$ci[3, 2]
robust_95_poly_con_df<-data.frame(cbind(robust_95_poly_est_con, robust_95_poly_ci_con_lower, robust_95_poly_ci_con_upper, '95%', 'conventional_poly'))
colnames(robust_95_poly_con_df)<-c('est', 'lower', 'upper', 'CI', 'type')
robust_95_poly_rob_df<-data.frame(cbind(robust_95_poly_est_rob, robust_95_poly_ci_rob_lower, robust_95_poly_ci_rob_upper, '95%', 'robust_poly'))
colnames(robust_95_poly_rob_df)<-c('est', 'lower', 'upper', 'CI', 'type')


#MDE is -.2
rdpower(dips73[c('victim', 'quotient_diff')], tau=-.195, h=3, cluster=dips73$Distrito, all=TRUE)

robust_90_bw3<-rdrobust(dips73$victim, dips73$quotient_diff, level=90, cluster=dips73$Distrito, h=3)
robust_90_bw3_est_con<-robust_90_bw3$coef[1]
robust_90_bw3_est_rob<-robust_90_bw3$coef[2]
robust_90_bw3_ci_con_lower<-robust_90_bw3$ci[1, 1]
robust_90_bw3_ci_con_upper<-robust_90_bw3$ci[1, 2]
robust_90_bw3_ci_rob_lower<-robust_90_bw3$ci[3, 1]
robust_90_bw3_ci_rob_upper<-robust_90_bw3$ci[3, 2]
robust_90_bw3_con_df<-data.frame(cbind(robust_90_bw3_est_con, robust_90_bw3_ci_con_lower, robust_90_bw3_ci_con_upper, '90%', 'conventional_bw3'))
colnames(robust_90_bw3_con_df)<-c('est', 'lower', 'upper', 'CI', 'type')
robust_90_bw3_rob_df<-data.frame(cbind(robust_90_bw3_est_rob, robust_90_bw3_ci_rob_lower, robust_90_bw3_ci_rob_upper, '90%', 'robust_bw3'))
colnames(robust_90_bw3_rob_df)<-c('est', 'lower', 'upper', 'CI', 'type')

#MDE is -.32
rdpower(dips73[c('victim', 'quotient_diff')], tau=-.32, h=3, cluster=dips73$Distrito, all=TRUE)

robust_95_bw3<-rdrobust(dips73$victim, dips73$quotient_diff, level=95, cluster=dips73$Distrito, h=3)
robust_95_bw3_est_con<-robust_95_bw3$coef[1]
robust_95_bw3_est_rob<-robust_95_bw3$coef[3]
robust_95_bw3_ci_con_lower<-robust_95_bw3$ci[1, 1]
robust_95_bw3_ci_con_upper<-robust_95_bw3$ci[1, 2]
robust_95_bw3_ci_rob_lower<-robust_95_bw3$ci[3, 1]
robust_95_bw3_ci_rob_upper<-robust_95_bw3$ci[3, 2]
robust_95_bw3_con_df<-data.frame(cbind(robust_95_bw3_est_con, robust_95_bw3_ci_con_lower, robust_95_bw3_ci_con_upper, '95%', 'conventional_bw3'))
colnames(robust_95_bw3_con_df)<-c('est', 'lower', 'upper', 'CI', 'type')
robust_95_bw3_rob_df<-data.frame(cbind(robust_95_bw3_est_rob, robust_95_bw3_ci_rob_lower, robust_95_bw3_ci_rob_upper, '95%', 'robust_bw3'))
colnames(robust_95_bw3_rob_df)<-c('est', 'lower', 'upper', 'CI', 'type')

#MDE is -.25
rdpower(dips73[c('victim', 'quotient_diff')], tau=-.25, h=3, p=2, cluster=dips73$Distrito, all=TRUE)

robust_90_bw3_poly<-rdrobust(dips73$victim, dips73$quotient_diff, level=90, cluster=dips73$Distrito, h=3, p=2)
robust_90_bw3_poly_est_con<-robust_90_bw3_poly$coef[1]
robust_90_bw3_poly_est_rob<-robust_90_bw3_poly$coef[3]
robust_90_bw3_poly_ci_con_lower<-robust_90_bw3_poly$ci[1, 1]
robust_90_bw3_poly_ci_con_upper<-robust_90_bw3_poly$ci[1, 2]
robust_90_bw3_poly_ci_rob_lower<-robust_90_bw3_poly$ci[3, 1]
robust_90_bw3_poly_ci_rob_upper<-robust_90_bw3_poly$ci[3, 2]
robust_90_bw3_poly_con_df<-data.frame(cbind(robust_90_bw3_poly_est_con, robust_90_bw3_poly_ci_con_lower, robust_90_bw3_poly_ci_con_upper, '90%', 'conventional_bw3_poly'))
colnames(robust_90_bw3_poly_con_df)<-c('est', 'lower', 'upper', 'CI', 'type')
robust_90_bw3_poly_rob_df<-data.frame(cbind(robust_90_bw3_poly_est_rob, robust_90_bw3_poly_ci_rob_lower, robust_90_bw3_poly_ci_rob_upper, '90%', 'robust_bw3_poly'))
colnames(robust_90_bw3_poly_rob_df)<-c('est', 'lower', 'upper', 'CI', 'type')

#MDE is -.38
rdpower(dips73[c('victim', 'quotient_diff')], tau=-.38, h=3, p=2, cluster=dips73$Distrito, all=TRUE)

robust_95_bw3_poly<-rdrobust(dips73$victim, dips73$quotient_diff, level=95, cluster=dips73$Distrito, h=3, p=2)
robust_95_bw3_poly_est_con<-robust_95_bw3_poly$coef[1]
robust_95_bw3_poly_est_rob<-robust_95_bw3_poly$coef[3]
robust_95_bw3_poly_ci_con_lower<-robust_95_bw3_poly$ci[1, 1]
robust_95_bw3_poly_ci_con_upper<-robust_95_bw3_poly$ci[1, 2]
robust_95_bw3_poly_ci_rob_lower<-robust_95_bw3_poly$ci[3, 1]
robust_95_bw3_poly_ci_rob_upper<-robust_95_bw3_poly$ci[3, 2]
robust_95_bw3_poly_con_df<-data.frame(cbind(robust_95_bw3_poly_est_con, robust_95_bw3_poly_ci_con_lower, robust_95_bw3_poly_ci_con_upper, '95%', 'conventional_bw3_poly'))
colnames(robust_95_bw3_poly_con_df)<-c('est', 'lower', 'upper', 'CI', 'type')
robust_95_bw3_poly_rob_df<-data.frame(cbind(robust_95_bw3_poly_est_rob, robust_95_bw3_poly_ci_rob_lower, robust_95_bw3_poly_ci_rob_upper, '95%', 'robust_bw3_poly'))
colnames(robust_95_bw3_poly_rob_df)<-c('est', 'lower', 'upper', 'CI', 'type')

robust_df<-rbind(robust_90_con_df, robust_90_rob_df, robust_95_con_df, robust_95_rob_df,
                 robust_90_cluster_con_df, robust_90_cluster_rob_df, robust_95_cluster_con_df, robust_95_cluster_rob_df,
                 robust_90_poly_con_df, robust_90_poly_rob_df, robust_95_poly_con_df, robust_95_poly_rob_df,
                 robust_90_bw3_con_df, robust_90_bw3_rob_df, robust_95_bw3_con_df, robust_95_bw3_rob_df,
                 robust_90_bw3_poly_con_df, robust_90_bw3_poly_rob_df, robust_95_bw3_poly_con_df, robust_95_bw3_poly_rob_df)
robust_df$est<-as.numeric(as.character(robust_df$est))
robust_df$lower<-as.numeric(as.character(robust_df$lower))
robust_df$upper<-as.numeric(as.character(robust_df$upper))
robust_df$type<-factor(robust_df$type, levels=c('robust_bw3_poly', 'conventional_bw3_poly',
                                                'robust_bw3', 'conventional_bw3', 'robust_poly',
                                                'conventional_poly', 'robust_cluster', 
                                                'conventional_cluster', 'robust', 'conventional'))

robust_plot<-ggplot(robust_df, aes(x=type, y=est, group=CI, linetype=CI, ymin=lower, ymax=upper))+
  theme_bw()+geom_pointrange(size=.5)+scale_linetype_manual(values=c('solid', 'dotted'),
                                                            guide = guide_legend(override.aes = list(
                                                              linetype = c("solid", "dotted"),
                                                              shape=c(NA, NA)) ))+
  theme(legend.position="bottom",  axis.title=element_text(size=16),
        text=element_text(family="Times New Roman", size=16),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+
  coord_flip() +
  geom_hline(aes(yintercept=0), lty=4) +
  ylab("") +
  xlab("")+scale_x_discrete(labels=c('Robust, Poly, BW=3, MDE=-.25', 'Conventional, Poly, BW=3, MDE=-.38', 
                                     'Robust, BW=3, MDE=-.20', 'Conventional, BW=3, MDE=-.32', 
                                     'Robust, Polynomial, MDE=-.28','Conventional, Polynomial, MDE=-.24',
                                     'Robust, Cluster, MDE=-.19', 'Conventional, Cluster, MDE=-.20',
                                     'Robust, MDE=-.28', 'Conventional, MDE=-.28'))

#ggsave('robust_plot.pdf', plot=robust_plot, height=4, width=6)




##########################################################
###############Table B13: Press coverage t-tests#########
#########################################################

ttest_presscoverage<-ttest_electedvunelected(dips73$CD_presscoverage, dips73)

##write(format(round(as.numeric(as.character(ttest_presscoverage$elected[1])), 2)), "mean_presscoverage_elected.tex")
##write(format(round(as.numeric(as.character(ttest_presscoverage$unelected[1])), 2)), "mean_presscoverage_unelected.tex")
##write(format(round(as.numeric(as.character(ttest_presscoverage$difference[1])), 2)), "mean_presscoverage_difference.tex")

ttest_presscoverage_nousopo<-ttest_electedvunelected(dips73_nousopo$CD_presscoverage, dips73_nousopo)

##write(format(round(as.numeric(as.character(ttest_presscoverage_nousopo$elected[1])), 2)), "mean_presscoverage_elected_nousopo.tex")
##write(format(round(as.numeric(as.character(ttest_presscoverage_nousopo$unelected[1])), 2)), "mean_presscoverage_unelected_nousopo.tex")
##write(format(round(as.numeric(as.character(ttest_presscoverage_nousopo$difference[1])), 2)), "mean_presscoverage_difference_nousopo.tex")

ttest_presscoverage_left<-ttest_electedvunelected(dips73_leftDC_nousopo$CD_presscoverage, dips73_leftDC_nousopo)

##write(format(round(as.numeric(as.character(ttest_presscoverage_left$elected[1])), 2)), "mean_presscoverage_elected_left_nousopo.tex")
##write(format(round(as.numeric(as.character(ttest_presscoverage_left$unelected[1])), 2)), "mean_presscoverage_unelected_left_nousopo.tex")
##write(format(round(as.numeric(as.character(ttest_presscoverage_left$difference[1])), 2)), "mean_presscoverage_difference_left_nousopo.tex")


#################################################################################
#####################Table B14: Press and election in close races (LLR)##########
#################################################################################

rdd_all_presscoverage<-RDestimate(CD_presscoverage~quotient_diff, data=dips73, cluster=dips73$Distrito)
rdd_all_party_presscoverage<-RDestimate(CD_presscoverage~quotient_diff|Partido, data=dips73, cluster=dips73$Distrito)

##write(format(round(rdd_all_presscoverage$est[1], 2)), "late_presscoverage.tex")
##write(format(round(rdd_all_presscoverage$est[2], 2)), "half_bw_presscoverage.tex")
##write(format(round(rdd_all_presscoverage$est[3], 2)), "double_bw_presscoverage.tex")

##write(format(round(rdd_all_party_presscoverage$est[1], 2)), "latep_presscoverage.tex")
##write(format(round(rdd_all_party_presscoverage$est[2], 2)), "halfp_presscoverage.tex")
##write(format(round(rdd_all_party_presscoverage$est[3], 2)), "doublep_presscoverage.tex")

##write(rdd_all_presscoverage$obs[1], "obs_late_presscoverage.tex")
##write(rdd_all_presscoverage$obs[2], "obs_half_late_presscoverage.tex")
##write(rdd_all_presscoverage$obs[3], "obs_double_late_presscoverage.tex")

##write(format(round(rdd_all_presscoverage$bw[1],2)), "bw_late_presscoverage.tex")
##write(format(round(rdd_all_presscoverage$bw[2],2)), "bw_half_late_presscoverage.tex")
##write(format(round(rdd_all_presscoverage$bw[3],2)), "bw_double_late_presscoverage.tex")

press_imbens<-bootstrapLLR(press_data, iterations)

late_lower_presscoverage<-format(round(quantile(press_imbens$LLR, probs=.05), 2))
late_upper_presscoverage<-format(round(quantile(press_imbens$LLR, probs=.95), 2))
late_lower_presscoverage_95<-format(round(quantile(press_imbens$LLR, probs=.025), 2))
late_upper_presscoverage_95<-format(round(quantile(press_imbens$LLR, probs=.975), 2))

##write(late_lower_presscoverage, "late_lower_presscoverage.tex")
##write(late_upper_presscoverage, "late_upper_presscoverage.tex")
##write(late_lower_presscoverage_95, "late_lower_presscoverage_95.tex")
##write(late_upper_presscoverage_95, "late_upper_presscoverage_95.tex")

late_half_lower_presscoverage<-format(round(quantile(press_imbens$LLRhalf, probs=.05), 2))
late_half_upper_presscoverage<-format(round(quantile(press_imbens$LLRhalf, probs=.95), 2))
late_half_lower_presscoverage_95<-format(round(quantile(press_imbens$LLRhalf, probs=.025), 2))
late_half_upper_presscoverage_95<-format(round(quantile(press_imbens$LLRhalf, probs=.975), 2))

##write(late_half_lower_presscoverage, "late_half_lower_presscoverage.tex")
##write(late_half_upper_presscoverage, "late_half_upper_presscoverage.tex")
##write(late_half_lower_presscoverage_95, "late_half_lower_presscoverage_95.tex")
##write(late_half_upper_presscoverage_95, "late_half_upper_presscoverage_95.tex")

late_2_lower_presscoverage<-format(round(quantile(press_imbens$LLR2, probs=.05), 2))
late_2_upper_presscoverage<-format(round(quantile(press_imbens$LLR2, probs=.95), 2))
late_2_lower_presscoverage_95<-format(round(quantile(press_imbens$LLR2, probs=.025), 2))
late_2_upper_presscoverage_95<-format(round(quantile(press_imbens$LLR2, probs=.975), 2))

##write(late_2_lower_presscoverage, "late_2_lower_presscoverage.tex")
##write(late_2_upper_presscoverage, "late_2_upper_presscoverage.tex")
##write(late_2_lower_presscoverage_95, "late_2_lower_presscoverage_95.tex")
##write(late_2_upper_presscoverage_95, "late_2_upper_presscoverage_95.tex")

late_party_lower_presscoverage<-format(round(quantile(press_imbens$LLRparty, probs=.05), 2))
late_party_upper_presscoverage<-format(round(quantile(press_imbens$LLRparty, probs=.95), 2))
late_party_lower_presscoverage_95<-format(round(quantile(press_imbens$LLRparty, probs=.025), 2))
late_party_upper_presscoverage_95<-format(round(quantile(press_imbens$LLRparty, probs=.975), 2))
##write(late_party_lower_presscoverage, "late_party_lower_presscoverage.tex")
##write(late_party_upper_presscoverage, "late_party_upper_presscoverage.tex")
##write(late_party_lower_presscoverage_95, "late_party_lower_presscoverage_95.tex")
##write(late_party_upper_presscoverage_95, "late_party_upper_presscoverage_95.tex")

late_party_half_lower_presscoverage<-format(round(quantile(press_imbens$LLRpartyhalf, probs=.05), 2))
late_party_half_upper_presscoverage<-format(round(quantile(press_imbens$LLRpartyhalf, probs=.95), 2))
late_party_half_lower_presscoverage_95<-format(round(quantile(press_imbens$LLRpartyhalf, probs=.025), 2))
late_party_half_upper_presscoverage_95<-format(round(quantile(press_imbens$LLRpartyhalf, probs=.975), 2))
##write(late_party_half_lower_presscoverage, "late_party_half_lower_presscoverage.tex")
##write(late_party_half_upper_presscoverage, "late_party_half_upper_presscoverage.tex")
##write(late_party_half_lower_presscoverage_95, "late_party_half_lower_presscoverage_95.tex")
##write(late_party_half_upper_presscoverage_95, "late_party_half_upper_presscoverage_95.tex")

late_party_2_lower_presscoverage<-format(round(quantile(press_imbens$LLRparty2, probs=.05), 2))
late_party_2_upper_presscoverage<-format(round(quantile(press_imbens$LLRparty2, probs=.95), 2))
late_party_2_lower_presscoverage_95<-format(round(quantile(press_imbens$LLRparty2, probs=.025), 2))
late_party_2_upper_presscoverage_95<-format(round(quantile(press_imbens$LLRparty2, probs=.975), 2))
##write(late_party_2_lower_presscoverage, "late_party_2_lower_presscoverage.tex")
##write(late_party_2_upper_presscoverage, "late_party_2_upper_presscoverage.tex")
##write(late_party_2_lower_presscoverage_95, "late_party_2_lower_presscoverage_95.tex")
##write(late_party_2_upper_presscoverage_95, "late_party_2_upper_presscoverage_95.tex")




########################################################################################
#########################################################################################
############################Alternative Explanations#####################################
#########################################################################################


###################################################
#######Table C1: Covariates and repression #######
##################################################
######Candidate covariates and physical coercion
alt1_coef=list()
alt1p_coef=list()
alt1pd_coef=list()

surname_results_all<-logitCoefficientsAlternative(dips73, iterations, 'dips73', 'surname')
dynastic_results_all<-logitCoefficientsAlternative(dips73, iterations, 'dips73', 'dynastic')
incumbent_results_all<-logitCoefficientsAlternative(dips73, iterations, 'dips73', 'incumbent_orpastelected')

surname_results_all<-plyr::compact(surname_results_all)
dynastic_results_all<-plyr::compact(dynastic_results_all)
incumbent_results_all<-plyr::compact(incumbent_results_all)

iterations_surname<-length(surname_results_all)/3
iterations_dynastic<-length(dynastic_results_all)/3
iterations_incumbent<-length(incumbent_results_all)/3

surname_results_logit=as.data.frame(t(matrix(unlist(surname_results_all[1:iterations_surname]), ncol=iterations_surname)))
surnamep_results_logit=as.data.frame(t(matrix(unlist(surname_results_all[(1+iterations_surname):(2*iterations_surname)]), ncol=iterations_surname)))
surnamepd_results_logit=as.data.frame(t(matrix(unlist(surname_results_all[(1+iterations_surname*2):(3*iterations_surname)]), ncol=iterations_surname)))

surname_reg<-glm(victim~surname, data=dips73_nousopo, family="binomial")
surname_effect<-format(round(summary(surname_reg)$coefficients[2,1], 2))
surname_AIC<-format(round(summary(surname_reg)$aic, 2))
surname_obs<-dim(dips73_nousopo)[1]
##write(surname_effect, "surname_effect.tex")
##write(surname_AIC, "surname_AIC.tex")
##write(surname_obs, "surname_obs.tex")


surname_lower<-format(round(quantile(surname_results_logit$V2,probs=.05), 2))
##write(surname_lower, "surname_lower.tex")
surname_upper<-format(round(quantile(surname_results_logit$V2,probs=.95), 2))
##write(surname_upper, "surname_upper.tex")

surname_constant<-format(round(summary(surname_reg)$coefficients[1,1], 2))
##write(surname_constant, "surname_constant.tex")
surname_constant_lower<-format(round(quantile(surname_results_logit$V1,probs=.05), 2))
##write(surname_constant_lower, "surname_constant_lower.tex")
surname_constant_upper<-format(round(quantile(surname_results_logit$V1,probs=.95), 2))
##write(surname_constant_upper, "surname_constant_upper.tex")


surnamep_reg<-glm(victim~surname+list, data=dips73_nousopo, family="binomial")
surnamep_effect<-format(round(summary(surnamep_reg)$coefficients[2,1], 2))
surnamep_AIC<-format(round(summary(surnamep_reg)$aic, 2))
surnamep_obs<-dim(dips73_nousopo)[1]
##write(surnamep_effect, "surnamep_effect.tex")
##write(surnamep_AIC, "surnamep_AIC.tex")
##write(surnamep_obs, "surnamep_obs.tex")

surnamep_lower<-format(round(quantile(surnamep_results_logit$V2,probs=.05), 2))
##write(surnamep_lower, "surnamep_lower.tex")
surnamep_upper<-format(round(quantile(surnamep_results_logit$V2,probs=.95), 2))
##write(surnamep_upper, "surnamep_upper.tex")

surnamep_constant<-format(round(summary(surnamep_reg)$coefficients[1,1], 2))
##write(surnamep_constant, "surnamep_constant.tex")
surnamep_constant_lower<-format(round(quantile(surnamep_results_logit$V1,probs=.05), 2))
##write(surnamep_constant_lower, "surnamep_constant_lower.tex")
surnamep_constant_upper<-format(round(quantile(surnamep_results_logit$V1,probs=.95), 2))
##write(surnamep_constant_upper, "surnamep_constant_upper.tex")



surnamepd_reg<-glm(victim~surname+list+factor(agrupacion), data=dips73_nousopo, family="binomial")
surnamepd_effect<-format(round(summary(surnamepd_reg)$coefficients[2,1], 2))
surnamepd_AIC<-format(round(summary(surnamepd_reg)$aic, 2))
surnamepd_obs<-dim(dips73_nousopo)[1]
##write(surnamepd_effect, "surnamepd_effect.tex")
##write(surnamepd_AIC, "surnamepd_AIC.tex")
##write(surnamepd_obs, "surnamepd_obs.tex")

surnamepd_lower<-format(round(quantile(surnamepd_results_logit$V2,probs=.05), 2))
##write(surnamepd_lower, "surnamepd_lower.tex")
surnamepd_upper<-format(round(quantile(surnamepd_results_logit$V2,probs=.95), 2))
##write(surnamepd_upper, "surnamepd_upper.tex")

surnamepd_constant<-format(round(summary(surnamepd_reg)$coefficients[1,1], 2))
##write(surnamepd_constant, "surnamepd_constant.tex")
surnamepd_constant_lower<-format(round(quantile(surnamepd_results_logit$V1,probs=.05), 2))
##write(surnamepd_constant_lower, "surnamepd_constant_lower.tex")
surnamepd_constant_upper<-format(round(quantile(surnamepd_results_logit$V1,probs=.95), 2))
##write(surnamepd_constant_upper, "surnamepd_constant_upper.tex")

##dynastic
dynastic_results_logit=as.data.frame(t(matrix(unlist(dynastic_results_all[1:iterations_dynastic]), ncol=iterations_dynastic)))
dynasticp_results_logit=as.data.frame(t(matrix(unlist(dynastic_results_all[(1+iterations_dynastic):(2*iterations_dynastic)]), ncol=iterations_dynastic)))
dynasticpd_results_logit=as.data.frame(t(matrix(unlist(dynastic_results_all[(1+iterations_dynastic*2):(3*iterations_dynastic)]), ncol=iterations_dynastic)))

dynastic_reg<-glm(victim~dynastic, data=dips73_nousopo, family="binomial")
dynastic_effect<-format(round(summary(dynastic_reg)$coefficients[2,1], 2))
dynastic_AIC<-format(round(summary(dynastic_reg)$aic, 2))
dynastic_obs<-dim(dips73_nousopo)[1]
##write(dynastic_effect, "dynastic_effect.tex")
##write(dynastic_AIC, "dynastic_AIC.tex")
##write(dynastic_obs, "dynastic_obs.tex")


dynastic_lower<-format(round(quantile(dynastic_results_logit$V2,probs=.05), 2))
##write(dynastic_lower, "dynastic_lower.tex")
dynastic_upper<-format(round(quantile(dynastic_results_logit$V2,probs=.95), 2))
##write(dynastic_upper, "dynastic_upper.tex")

dynastic_constant<-format(round(summary(dynastic_reg)$coefficients[1,1], 2))
##write(dynastic_constant, "dynastic_constant.tex")
dynastic_constant_lower<-format(round(quantile(dynastic_results_logit$V1,probs=.05), 2))
##write(dynastic_constant_lower, "dynastic_constant_lower.tex")
dynastic_constant_upper<-format(round(quantile(dynastic_results_logit$V1,probs=.95), 2))
##write(dynastic_constant_upper, "dynastic_constant_upper.tex")


dynasticp_reg<-glm(victim~dynastic+list, data=dips73_nousopo, family="binomial")
dynasticp_effect<-format(round(summary(dynasticp_reg)$coefficients[2,1], 2))
dynasticp_AIC<-format(round(summary(dynasticp_reg)$aic, 2))
dynasticp_obs<-dim(dips73_nousopo)[1]
##write(dynasticp_effect, "dynasticp_effect.tex")
##write(dynasticp_AIC, "dynasticp_AIC.tex")
##write(dynasticp_obs, "dynasticp_obs.tex")

dynasticp_lower<-format(round(quantile(dynasticp_results_logit$V2,probs=.05), 2))
##write(dynasticp_lower, "dynasticp_lower.tex")
dynasticp_upper<-format(round(quantile(dynasticp_results_logit$V2,probs=.95), 2))
##write(dynasticp_upper, "dynasticp_upper.tex")

dynasticp_constant<-format(round(summary(dynasticp_reg)$coefficients[1,1], 2))
##write(dynasticp_constant, "dynasticp_constant.tex")
dynasticp_constant_lower<-format(round(quantile(dynasticp_results_logit$V1,probs=.05), 2))
##write(dynasticp_constant_lower, "dynasticp_constant_lower.tex")
dynasticp_constant_upper<-format(round(quantile(dynasticp_results_logit$V1,probs=.95), 2))
##write(dynasticp_constant_upper, "dynasticp_constant_upper.tex")



dynasticpd_reg<-glm(victim~dynastic+list+factor(agrupacion), data=dips73_nousopo, family="binomial")
dynasticpd_effect<-format(round(summary(dynasticpd_reg)$coefficients[2,1], 2))
dynasticpd_AIC<-format(round(summary(dynasticpd_reg)$aic, 2))
dynasticpd_obs<-dim(dips73_nousopo)[1]
##write(dynasticpd_effect, "dynasticpd_effect.tex")
##write(dynasticpd_AIC, "dynasticpd_AIC.tex")
##write(dynasticpd_obs, "dynasticpd_obs.tex")

dynasticpd_lower<-format(round(quantile(dynasticpd_results_logit$V2,probs=.05), 2))
##write(dynasticpd_lower, "dynasticpd_lower.tex")
dynasticpd_upper<-format(round(quantile(dynasticpd_results_logit$V2,probs=.95), 2))
##write(dynasticpd_upper, "dynasticpd_upper.tex")

dynasticpd_constant<-format(round(summary(dynasticpd_reg)$coefficients[1,1], 2))
##write(dynasticpd_constant, "dynasticpd_constant.tex")
dynasticpd_constant_lower<-format(round(quantile(dynasticpd_results_logit$V1,probs=.05), 2))
##write(dynasticpd_constant_lower, "dynasticpd_constant_lower.tex")
dynasticpd_constant_upper<-format(round(quantile(dynasticpd_results_logit$V1,probs=.95), 2))
##write(dynasticpd_constant_upper, "dynasticpd_constant_upper.tex")


#incumbent
incumbent_results_logit=as.data.frame(t(matrix(unlist(incumbent_results_all[1:iterations_incumbent]), ncol=iterations_incumbent)))
incumbentp_results_logit=as.data.frame(t(matrix(unlist(incumbent_results_all[(1+iterations_incumbent):(2*iterations_incumbent)]), ncol=iterations_incumbent)))
incumbentpd_results_logit=as.data.frame(t(matrix(unlist(incumbent_results_all[(1+2*iterations_incumbent):(3*iterations_incumbent)]), ncol=iterations_incumbent)))

incumbent_reg<-glm(victim~incumbent_orpastelected, data=dips73_nousopo, family="binomial")
incumbent_effect<-format(round(summary(incumbent_reg)$coefficients[2,1], 2))
incumbent_AIC<-format(round(summary(incumbent_reg)$aic, 2))
incumbent_obs<-dim(dips73_nousopo)[1]
##write(incumbent_effect, "incumbent_effect.tex")
##write(incumbent_AIC, "incumbent_AIC.tex")
##write(incumbent_obs, "incumbent_obs.tex")


incumbent_lower<-format(round(quantile(incumbent_results_logit$V2,probs=.05), 2))
##write(incumbent_lower, "incumbent_lower.tex")
incumbent_upper<-format(round(quantile(incumbent_results_logit$V2,probs=.95), 2))
##write(incumbent_upper, "incumbent_upper.tex")

incumbent_constant<-format(round(summary(incumbent_reg)$coefficients[1,1], 2))
##write(incumbent_constant, "incumbent_constant.tex")
incumbent_constant_lower<-format(round(quantile(incumbent_results_logit$V1,probs=.05), 2))
##write(incumbent_constant_lower, "incumbent_constant_lower.tex")
incumbent_constant_upper<-format(round(quantile(incumbent_results_logit$V1,probs=.95), 2))
##write(incumbent_constant_upper, "incumbent_constant_upper.tex")


incumbentp_reg<-glm(victim~incumbent_orpastelected+list, data=dips73_nousopo, family="binomial")
incumbentp_effect<-format(round(summary(incumbentp_reg)$coefficients[2,1], 2))
incumbentp_AIC<-format(round(summary(incumbentp_reg)$aic, 2))
incumbentp_obs<-dim(dips73_nousopo)[1]
##write(incumbentp_effect, "incumbentp_effect.tex")
##write(incumbentp_AIC, "incumbentp_AIC.tex")
##write(incumbentp_obs, "incumbentp_obs.tex")

incumbentp_lower<-format(round(quantile(incumbentp_results_logit$V2,probs=.05), 2))
##write(incumbentp_lower, "incumbentp_lower.tex")
incumbentp_upper<-format(round(quantile(incumbentp_results_logit$V2,probs=.95), 2))
##write(incumbentp_upper, "incumbentp_upper.tex")

incumbentp_constant<-format(round(summary(incumbentp_reg)$coefficients[1,1], 2))
##write(incumbentp_constant, "incumbentp_constant.tex")
incumbentp_constant_lower<-format(round(quantile(incumbentp_results_logit$V1,probs=.05), 2))
##write(incumbentp_constant_lower, "incumbentp_constant_lower.tex")
incumbentp_constant_upper<-format(round(quantile(incumbentp_results_logit$V1,probs=.95), 2))
##write(incumbentp_constant_upper, "incumbentp_constant_upper.tex")


incumbentpd_reg<-glm(victim~incumbent_orpastelected+list+factor(agrupacion), data=dips73_nousopo, family="binomial")
incumbentpd_effect<-format(round(summary(incumbentpd_reg)$coefficients[2,1], 2))
incumbentpd_AIC<-format(round(summary(incumbentpd_reg)$aic, 2))
incumbentpd_obs<-dim(dips73_nousopo)[1]
##write(incumbentpd_effect, "incumbentpd_effect.tex")
##write(incumbentpd_AIC, "incumbentpd_AIC.tex")
##write(incumbentpd_obs, "incumbentpd_obs.tex")

incumbentpd_lower<-format(round(quantile(incumbentpd_results_logit$V2,probs=.05), 2))
##write(incumbentpd_lower, "incumbentpd_lower.tex")
incumbentpd_upper<-format(round(quantile(incumbentpd_results_logit$V2,probs=.95), 2))
##write(incumbentpd_upper, "incumbentpd_upper.tex")

incumbentpd_constant<-format(round(summary(incumbentpd_reg)$coefficients[1,1], 2))
##write(incumbentpd_constant, "incumbentpd_constant.tex")
incumbentpd_constant_lower<-format(round(quantile(incumbentpd_results_logit$V1,probs=.05), 2))
##write(incumbentpd_constant_lower, "incumbentpd_constant_lower.tex")
incumbentpd_constant_upper<-format(round(quantile(incumbentpd_results_logit$V1,probs=.95), 2))
##write(incumbentpd_constant_upper, "incumbentpd_constant_upper.tex")




##################################################################
#############Figure C1: T-tests excluding asylum seekers ##########
##################################################################


ttest_noasylum_anyrepression<-ttest_electedvunelected(dips73$anyrepression, dips73)
ttest_noasylum_anyrepression$type<-'3. anyrepression_all'

ttest_noasylum_anyrepression_nousopo<-ttest_electedvunelected(dips73_nousopo$anyrepression, dips73_nousopo)
ttest_noasylum_anyrepression_nousopo$type<-'2. anyrepression_nousopo'

ttest_noasylum_anyrepression_leftDC<-ttest_electedvunelected(dips73_leftDC_nousopo$anyrepression, dips73_leftDC_nousopo)
ttest_noasylum_anyrepression_leftDC$type<-'1. anyrepression_left'

anyrepression_ttest_noasylum_df<-rbind(ttest_noasylum_anyrepression, ttest_noasylum_anyrepression_nousopo, ttest_noasylum_anyrepression_leftDC)
colnames(anyrepression_ttest_noasylum_df)<-c('elected', 'unelected', 'difference', 'lower', 'upper', 'CI', 'type')


anyrepression_ttest_noasylum_plot<-ggplot(anyrepression_ttest_noasylum_df, aes(x=type, y=as.numeric(as.character(difference)), group=CI, linetype=CI))+
  geom_point()+
  theme_bw()+geom_pointrange(size=.5, ymin=as.numeric(as.character(anyrepression_ttest_noasylum_df$lower)), 
                             ymax=as.numeric(as.character(anyrepression_ttest_noasylum_df$upper)))+
  ylim(-10, 30)+scale_linetype_manual(values=c('solid', 'dotted'),
                                      guide = guide_legend(override.aes = list(
                                        linetype = c("solid", "dotted"),
                                        shape=c(NA, NA)) ))+
  theme(legend.position="bottom",  axis.title=element_text(size=16),
        text=element_text(family="Times New Roman", size=16),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+
  coord_flip() +
  geom_hline(aes(yintercept=0), lty=4) +
  ylab("") +
  xlab("")+scale_x_discrete(labels=c('UP and PDC', 'Excluding USOPO', 'All Candidates'))
#ggsave('anyrepression_ttest_noasylum_plot.pdf', plot=anyrepression_ttest_noasylum_plot, height=4, width=5)



ttest_noasylum_victim<-ttest_electedvunelected(dips73$victim, dips73)
ttest_noasylum_victim$type<-'3. victim_all'

ttest_noasylum_victim_nousopo<-ttest_electedvunelected(dips73_nousopo$victim, dips73_nousopo)
ttest_noasylum_victim_nousopo$type<-'2. victim_nousopo'

ttest_noasylum_victim_leftDC<-ttest_electedvunelected(dips73_leftDC_nousopo$victim, dips73_leftDC_nousopo)
ttest_noasylum_victim_leftDC$type<-'1. victim_left'

victim_ttest_noasylum_df<-rbind(ttest_noasylum_victim, ttest_noasylum_victim_nousopo, ttest_noasylum_victim_leftDC)
colnames(victim_ttest_noasylum_df)<-c('elected', 'unelected', 'difference', 'lower', 'upper', 'CI', 'type')

physicalcoercion_ttest_noasylum_plot<-ggplot(victim_ttest_noasylum_df, aes(x=type, y=as.numeric(as.character(difference)), group=CI, linetype=CI))+
  geom_point()+
  theme_bw()+geom_pointrange(size=.5, ymin=as.numeric(as.character(victim_ttest_noasylum_df$lower)), 
                             ymax=as.numeric(as.character(victim_ttest_noasylum_df$upper)))+
  ylim(-25, 10)+scale_linetype_manual(values=c('solid', 'dotted'),
                                      guide = guide_legend(override.aes = list(
                                        linetype = c("solid", "dotted"),
                                        shape=c(NA, NA)) ))+
  theme(legend.position="bottom",  axis.title=element_text(size=16),
        text=element_text(family="Times New Roman", size=16),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+
  coord_flip() +
  geom_hline(aes(yintercept=0), lty=4) +
  scale_x_discrete(labels=c('UP and PDC', 'Excluding USOPO', 'All Candidates'))

#ggsave('physicalcoercion_ttest_noasylum_plot.pdf', plot=physicalcoercion_ttest_noasylum_plot, height=4, width=5)




########################################################################
##################Table C2 & C3: Logit excluding asylum seekers#########
#######################################################################
summary(lm(anyrepression~vote_perc+factor(agrupacion)+Partido, data=dips69))


#Overall repression, model 1 -- no controls
ar_logit_noc_noa<-logitCoefficients(dips73_noasylum, c('anyrepression', 'vote_perc'), iterations)
functional_iterations=length(unlist(ar_logit_noc_noa))/2

elected_noasylum_anyrepression_results=as.data.frame(t(matrix(unlist(ar_logit_noc_noa), ncol=functional_iterations)))
elected_noasylum_anyrepression_reg<-glm(anyrepression~vote_perc, data=dips73_noasylum, family="binomial")
elected_noasylum_elected_noasylum_anyrepression<-format(round(summary(elected_noasylum_anyrepression_reg)$coefficients[2,1], 2))
##write(elected_noasylum_elected_noasylum_anyrepression, "elected_noasylum_elected_noasylum_anyrepression.tex")
elected_noasylum_anyrepression_AIC<-format(round(summary(elected_noasylum_anyrepression_reg)$aic, 2))
##write(elected_noasylum_anyrepression_AIC, "elected_noasylum_anyrepression_AIC.tex")

elected_noasylum_anyrepression_lower<-format(round(quantile(elected_noasylum_anyrepression_results$V2,probs=.05), 2))
##write(elected_noasylum_anyrepression_lower, "elected_noasylum_anyrepression_lower.tex")
elected_noasylum_anyrepression_upper<-format(round(quantile(elected_noasylum_anyrepression_results$V2,probs=.95), 2))
##write(elected_noasylum_anyrepression_upper, "elected_noasylum_anyrepression_upper.tex")

elected_noasylum_constant_anyrepression<-format(round(summary(elected_noasylum_anyrepression_reg)$coefficients[1,1], 2))
##write(elected_noasylum_constant_anyrepression, "elected_noasylum_constant_anyrepression.tex")
elected_noasylum_constant_anyrepression_lower<-format(round(quantile(elected_noasylum_anyrepression_results$V1,probs=.05), 2))
##write(elected_noasylum_constant_anyrepression_lower, "elected_noasylum_constant_anyrepression_lower.tex")
elected_noasylum_constant_anyrepression_upper<-format(round(quantile(elected_noasylum_anyrepression_results$V1,probs=.95), 2))
##write(elected_noasylum_constant_anyrepression_upper, "elected_noasylum_constant_anyrepression_upper.tex")

##Overall repression, model 2 --  controls
ar_logit_c_noa<-logitCoefficients(dips73_noasylum, c('anyrepression', 'vote_perc', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)
functional_iterations=length(unlist(ar_logit_c_noa))/2

electedc_noasylum_anyrepression_results=as.data.frame(t(matrix(unlist(ar_logit_c_noa), ncol=functional_iterations)))

electedc_noasylum_anyrepression_reg<-glm(anyrepression~vote_perc+surname+incumbent_orpastelected+dynastic, data=dips73_noasylum, family="binomial")
electedc_noasylum_elected_noasylum_anyrepression<-format(round(summary(electedc_noasylum_anyrepression_reg)$coefficients[2,1], 2))
##write(electedc_noasylum_elected_noasylum_anyrepression, "electedc_noasylum_elected_noasylum_anyrepression.tex")
electedc_noasylum_anyrepression_AIC<-format(round(summary(electedc_noasylum_anyrepression_reg)$aic, 2))
##write(electedc_noasylum_anyrepression_AIC, "electedc_noasylum_anyrepression_AIC.tex")

electedc_noasylum_anyrepression_lower<-format(round(quantile(electedc_noasylum_anyrepression_results$V2,probs=.05), 2))
##write(electedc_noasylum_anyrepression_lower, "electedc_noasylum_anyrepression_lower.tex")
electedc_noasylum_anyrepression_upper<-format(round(quantile(electedc_noasylum_anyrepression_results$V2,probs=.95), 2))
##write(electedc_noasylum_anyrepression_upper, "electedc_noasylum_anyrepression_upper.tex")

electedc_noasylum_constant_anyrepression<-format(round(summary(electedc_noasylum_anyrepression_reg)$coefficients[1,1], 2))
##write(electedc_noasylum_constant_anyrepression, "electedc_noasylum_constant_anyrepression.tex")
electedc_noasylum_constant_anyrepression_lower<-format(round(quantile(electedc_noasylum_anyrepression_results$V1,probs=.05), 2))
##write(electedc_noasylum_constant_anyrepression_lower, "electedc_noasylum_constant_anyrepression_lower.tex")
electedc_noasylum_constant_anyrepression_upper<-format(round(quantile(electedc_noasylum_anyrepression_results$V1,probs=.95), 2))
##write(electedc_noasylum_constant_anyrepression_upper, "electedc_noasylum_constant_anyrepression_upper.tex")

##Overall repression, model 3 -- full controls
dips73_noasylum$agrupacion_factor<-as.factor(dips73_noasylum$agrupacion)
dips73_noasylum$list_factor<-as.factor(dips73_noasylum$list)

ar_logit_pdc_noa<-logitCoefficients(dips73_noasylum, c('anyrepression', 'vote_perc', 'surname', 'incumbent_orpastelected', 'dynastic', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(ar_logit_pdc_noa))/2

electedpdc_noasylum_anyrepression_results=as.data.frame(t(matrix(unlist(ar_logit_pdc_noa), ncol=functional_iterations)))
electedpdc_noasylum_anyrepression_reg<-glm(anyrepression~vote_perc+factor(list)+factor(agrupacion)+surname+dynastic+incumbent_orpastelected, data=dips73_noasylum, family="binomial")

electedpdc_noasylum_elected_noasylum_anyrepression<-format(round(summary(electedpdc_noasylum_anyrepression_reg)$coefficients[2,1], 2))
##write(electedpdc_noasylum_elected_noasylum_anyrepression, "electedpdc_noasylum_elected_noasylum_anyrepression.tex")
electedpdc_noasylum_anyrepression_AIC<-format(round(summary(electedpdc_noasylum_anyrepression_reg)$aic, 2))
##write(electedpdc_noasylum_anyrepression_AIC, "electedpdc_noasylum_anyrepression_AIC.tex")

electedpdc_noasylum_anyrepression_lower<-format(round(quantile(electedpdc_noasylum_anyrepression_results$V2,probs=.05), 2))
##write(electedpdc_noasylum_anyrepression_lower, "electedpdc_noasylum_anyrepression_lower.tex")
electedpdc_noasylum_anyrepression_upper<-format(round(quantile(electedpdc_noasylum_anyrepression_results$V2,probs=.95), 2))
##write(electedpdc_noasylum_anyrepression_upper, "electedpdc_noasylum_anyrepression_upper.tex")

electedpdc_noasylum_constant_anyrepression<-format(round(summary(electedpdc_noasylum_anyrepression_reg)$coefficients[1,1], 2))
##write(electedpdc_noasylum_constant_anyrepression, "electedpdc_noasylum_constant_anyrepression.tex")
electedpdc_noasylum_constant_anyrepression_lower<-format(round(quantile(electedpdc_noasylum_anyrepression_results$V1,probs=.05), 2))
##write(electedpdc_noasylum_constant_anyrepression_lower, "electedpdc_noasylum_constant_anyrepression_lower.tex")
electedpdc_noasylum_constant_anyrepression_upper<-format(round(quantile(electedpdc_noasylum_anyrepression_results$V1,probs=.95), 2))
##write(electedpdc_noasylum_constant_anyrepression_upper, "electedpdc_noasylum_constant_anyrepression_upper.tex")

###Physical coercion, model 1 -- no controls
pc_logit_noc_noa<-logitCoefficients(dips73_noasylum, c('victim', 'elected'), iterations)
functional_iterations=length(unlist(pc_logit_noc_noa))/2

elected_noasylum_results=as.data.frame(t(matrix(unlist(pc_logit_noc_noa), ncol=functional_iterations)))

elected_noasylum_reg<-glm(victim~factor(elected), data=dips73_noasylum, family="binomial")
elected_noasylum_elected<-format(round(summary(elected_noasylum_reg)$coefficients[2,1], 2))
elected_noasylum_AIC<-format(round(summary(elected_noasylum_reg)$aic, 2))
elected_noasylum_obs<-dim(dips73)[1]
##write(elected_noasylum_elected, "elected_noasylum_elected.tex")
##write(elected_noasylum_AIC, "elected_noasylum_AIC.tex")
##write(elected_noasylum_obs, "elected_noasylum_obs.tex")

elected_noasylum_lower<-format(round(quantile(elected_noasylum_results$V2,probs=.05), 2))
##write(elected_noasylum_lower, "elected_noasylum_lower.tex")
elected_noasylum_upper<-format(round(quantile(elected_noasylum_results$V2,probs=.95), 2))
##write(elected_noasylum_upper, "elected_noasylum_upper.tex")

elected_noasylum_constant<-format(round(summary(elected_noasylum_reg)$coefficients[1,1], 2))
##write(elected_noasylum_constant, "elected_noasylum_constant.tex")

elected_noasylum_constant_lower<-format(round(quantile(elected_noasylum_results$V1,probs=.05), 2))
##write(elected_noasylum_constant_lower, "elected_noasylum_constant_lower.tex")
elected_noasylum_constant_upper<-format(round(quantile(elected_noasylum_results$V1,probs=.95), 2))
##write(elected_noasylum_constant_upper, "elected_noasylum_constant_upper.tex")

##Physical coercion, model 2 -- controls
pc_logit_c_noa<-logitCoefficients(dips73_noasylum, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)
functional_iterations=length(unlist(pc_logit_c_noa))/2

electedc_noasylum_results=as.data.frame(t(matrix(unlist(pc_logit_c_noa), ncol=functional_iterations)))

electedc_noasylum_glm<-glm(victim~factor(elected)+surname+incumbent_orpastelected+dynastic, data=dips73_noasylum, family="binomial")
electedc_noasylum_elected<-format(round(summary(electedc_noasylum_glm)$coefficients[2,1], 2))
##write(electedc_noasylum_elected, "electedc_noasylum_elected.tex")

electedc_noasylum_AIC<-format(round(summary(electedc_noasylum_glm)$aic, 2))
##write(electedc_noasylum_AIC, "electedc_noasylum_AIC.tex")
electedc_noasylum_lower<-format(round(quantile(electedc_noasylum_results$V2,probs=.05), 2))
##write(electedc_noasylum_lower, "electedc_noasylum_lower.tex")
electedc_noasylum_upper<-format(round(quantile(electedc_noasylum_results$V2,probs=.95), 2))
##write(electedc_noasylum_upper, "electedc_noasylum_upper.tex")

electedc_noasylum_constant<-format(round(summary(electedc_noasylum_glm)$coefficients[1,1], 2))
##write(electedc_noasylum_constant, "electedc_noasylum_constant.tex")
electedc_noasylum_constant_lower<-format(round(quantile(electedc_noasylum_results$V1,probs=.05), 2))
##write(electedc_noasylum_constant_lower, "electedc_noasylum_constant_lower.tex")
electedc_noasylum_constant_upper<-format(round(quantile(electedc_noasylum_results$V1,probs=.95), 2))
##write(electedc_noasylum_constant_upper, "electedc_noasylum_constant_upper.tex")


##Physical coercion, model 3 -- full controls
pc_logit_pdc_noa<-logitCoefficients(dips73_noasylum, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(pc_logit_pdc_noa))/2

electedpdc_noasylum_results=as.data.frame(t(matrix(unlist(pc_logit_pdc_noa), ncol=functional_iterations)))
electedpdc_noasylum_glm<-glm(victim~elected+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73_noasylum, family='binomial')
electedpdc_noasylum_elected<-format(round(summary(electedpdc_noasylum_glm)$coefficients[2,1], 2))
electedpdc_noasylum_AIC<-format(round(summary(electedpdc_noasylum_glm)$aic, 2))
##write(electedpdc_noasylum_AIC, "electedpdc_noasylum_AIC.tex")
##write(electedpdc_noasylum_elected, "electedpdc_noasylum_elected.tex")
electedpdc_noasylum_lower<-format(round(quantile(electedpdc_noasylum_results$V2,probs=.05), 2))
##write(electedpdc_noasylum_lower, "electedpdc_noasylum_lower.tex")
electedpdc_noasylum_upper<-format(round(quantile(electedpdc_noasylum_results$V2,probs=.95), 2))
##write(electedpdc_noasylum_upper, "electedpdc_noasylum_upper.tex")

electedpdc_noasylum_constant<-format(round(summary(electedpdc_noasylum_glm)$coefficients[1,1], 2))
##write(electedpdc_noasylum_constant, "electedpdc_noasylum_constant.tex")
electedpdc_noasylum_constant_lower<-format(round(quantile(electedpdc_noasylum_results$V1,probs=.05), 2))
##write(electedpdc_noasylum_constant_lower, "electedpdc_noasylum_constant_lower.tex")
electedpdc_noasylum_constant_upper<-format(round(quantile(electedpdc_noasylum_results$V1,probs=.95), 2))
##write(electedpdc_noasylum_constant_upper, "electedpdc_noasylum_constant_upper.tex")


###Logistic regression, excluding USOPO

##Any repression, model 1 -- no controls
ar_logit_noc_noa_nousopo<-logitCoefficients(dips73_nousopo_noasylum, c('anyrepression', 'vote_perc'), iterations)
functional_iterations=length(unlist(ar_logit_noc_noa_nousopo))/2

elected_noasylum_anyrepression_logit_noasylum_results_nousopo=as.data.frame(t(matrix(unlist(ar_logit_noc_noa_nousopo), ncol=functional_iterations)))
elected_noasylum_anyrepression_nousopo_reg<-glm(anyrepression~vote_perc, data=dips73_nousopo_noasylum, family="binomial")
elected_noasylum_elected_noasylum_anyrepression_nousopo<-format(round(summary(elected_noasylum_anyrepression_nousopo_reg)$coefficients[2,1], 2))
##write(elected_noasylum_elected_noasylum_anyrepression_nousopo, "elected_noasylum_elected_noasylum_anyrepression_nousopo.tex")
elected_noasylum_anyrepression_nousopo_AIC<-format(round(summary(elected_noasylum_anyrepression_nousopo_reg)$aic, 2))
##write(elected_noasylum_anyrepression_nousopo_AIC, "elected_noasylum_anyrepression_nousopo_AIC.tex")

elected_noasylum_anyrepression_lower_nousopo<-format(round(quantile(elected_noasylum_anyrepression_logit_noasylum_results_nousopo$V2,probs=.05), 2))
##write(elected_noasylum_anyrepression_lower_nousopo, "elected_noasylum_anyrepression_lower_nousopo.tex")
elected_noasylum_anyrepression_upper_nousopo<-format(round(quantile(elected_noasylum_anyrepression_logit_noasylum_results_nousopo$V2,probs=.95), 2))
##write(elected_noasylum_anyrepression_upper_nousopo, "elected_noasylum_anyrepression_upper_nousopo.tex")

elected_noasylum_constant_anyrepression_nousopo<-format(round(summary(elected_noasylum_anyrepression_nousopo_reg)$coefficients[1,1], 2))
##write(elected_noasylum_constant_anyrepression_nousopo, "elected_noasylum_constant_anyrepression_nousopo.tex")
elected_noasylum_constant_anyrepression_lower_nousopo<-format(round(quantile(elected_noasylum_anyrepression_logit_noasylum_results_nousopo$V1,probs=.05), 2))
##write(elected_noasylum_constant_anyrepression_lower_nousopo, "elected_noasylum_constant_anyrepression_lower_nousopo.tex")
elected_noasylum_constant_anyrepression_upper_nousopo<-format(round(quantile(elected_noasylum_anyrepression_logit_noasylum_results_nousopo$V1,probs=.95), 2))
##write(elected_noasylum_constant_anyrepression_upper_nousopo, "elected_noasylum_constant_anyrepression_upper_nousopo.tex")

##Any repression, model 2 -- party controls
ar_logit_c_noa_nousopo<-logitCoefficients(dips73_nousopo_noasylum, c('anyrepression', 'vote_perc', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)
functional_iterations=length(unlist(ar_logit_c_noa_nousopo))/2

electedc_noasylum_anyrepression_logit_noasylum_results_nousopo=as.data.frame(t(matrix(unlist(ar_logit_c_noa_nousopo), ncol=functional_iterations)))

electedc_noasylum_anyrepression_nousopo_reg<-glm(anyrepression~vote_perc+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo_noasylum, family="binomial")
electedc_noasylum_elected_noasylum_anyrepression_nousopo<-format(round(summary(electedc_noasylum_anyrepression_nousopo_reg)$coefficients[2,1], 2))
##write(electedc_noasylum_elected_noasylum_anyrepression_nousopo, "electedc_noasylum_elected_noasylum_anyrepression_nousopo.tex")
electedc_noasylum_anyrepression_nousopo_AIC<-format(round(summary(electedc_noasylum_anyrepression_nousopo_reg)$aic, 2))
##write(electedc_noasylum_anyrepression_nousopo_AIC, "electedc_noasylum_anyrepression_nousopo_AIC.tex")

electedc_noasylum_anyrepression_lower_nousopo<-format(round(quantile(electedc_noasylum_anyrepression_logit_noasylum_results_nousopo$V2,probs=.05), 2))
##write(electedc_noasylum_anyrepression_lower_nousopo, "electedc_noasylum_anyrepression_lower_nousopo.tex")
electedc_noasylum_anyrepression_upper_nousopo<-format(round(quantile(electedc_noasylum_anyrepression_logit_noasylum_results_nousopo$V2,probs=.95), 2))
##write(electedc_noasylum_anyrepression_upper_nousopo, "electedc_noasylum_anyrepression_upper_nousopo.tex")

electedc_noasylum_constant_anyrepression_nousopo<-format(round(summary(electedc_noasylum_anyrepression_nousopo_reg)$coefficients[1,1], 2))
##write(electedc_noasylum_constant_anyrepression_nousopo, "electedc_noasylum_constant_anyrepression_nousopo.tex")
electedc_noasylum_constant_anyrepression_lower_nousopo<-format(round(quantile(electedc_noasylum_anyrepression_logit_noasylum_results_nousopo$V1,probs=.05), 2))
##write(electedc_noasylum_constant_anyrepression_lower_nousopo, "electedc_noasylum_constant_anyrepression_lower_nousopo.tex")
electedc_noasylum_constant_anyrepression_upper_nousopo<-format(round(quantile(electedc_noasylum_anyrepression_logit_noasylum_results_nousopo$V1,probs=.95), 2))
##write(electedc_noasylum_constant_anyrepression_upper_nousopo, "electedc_noasylum_constant_anyrepression_upper_nousopo.tex")

##Any repression -- all covariates
dips73_nousopo_noasylum$agrupacion_factor<-as.factor(dips73_nousopo_noasylum$agrupacion)

ar_logit_pdc_noa_nousopo<-logitCoefficients(dips73_nousopo_noasylum, c('anyrepression', 'vote_perc', 'surname', 'incumbent_orpastelected', 'dynastic', 'agrupacion_factor', 'list'), iterations)
functional_iterations=length(unlist(ar_logit_pdc_noa_nousopo))/2

electedpdc_noasylum_anyrepression_logit_noasylum_results_nousopo=as.data.frame(t(matrix(unlist(ar_logit_pdc_noa_nousopo), ncol=functional_iterations)))

electedpdc_noasylum_anyrepression_nousopo_reg<-glm(anyrepression~vote_perc+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo_noasylum, family="binomial")
electedpdc_noasylum_elected_noasylum_anyrepression_nousopo<-format(round(summary(electedpdc_noasylum_anyrepression_nousopo_reg)$coefficients[2,1], 2))
##write(electedpdc_noasylum_elected_noasylum_anyrepression_nousopo, "electedpdc_noasylum_elected_noasylum_anyrepression_nousopo.tex")
electedpdc_noasylum_anyrepression_nousopo_AIC<-format(round(summary(electedpdc_noasylum_anyrepression_nousopo_reg)$aic, 2))
##write(electedpdc_noasylum_anyrepression_nousopo_AIC, "electedpdc_noasylum_anyrepression_nousopo_AIC.tex")

electedpdc_noasylum_anyrepression_lower_nousopo<-format(round(quantile(electedpdc_noasylum_anyrepression_logit_noasylum_results_nousopo$V2,probs=.05), 2))
##write(electedpdc_noasylum_anyrepression_lower_nousopo, "electedpdc_noasylum_anyrepression_lower_nousopo.tex")
electedpdc_noasylum_anyrepression_upper_nousopo<-format(round(quantile(electedpdc_noasylum_anyrepression_logit_noasylum_results_nousopo$V2,probs=.95), 2))
##write(electedpdc_noasylum_anyrepression_upper_nousopo, "electedpdc_noasylum_anyrepression_upper_nousopo.tex")

electedpdc_noasylum_constant_anyrepression_nousopo<-format(round(summary(electedpdc_noasylum_anyrepression_nousopo_reg)$coefficients[1,1], 2))
##write(electedpdc_noasylum_constant_anyrepression_nousopo, "electedpdc_noasylum_constant_anyrepression_nousopo.tex")
electedpdc_noasylum_constant_anyrepression_lower_nousopo<-format(round(quantile(electedpdc_noasylum_anyrepression_logit_noasylum_results_nousopo$V1,probs=.05), 2))
##write(electedpdc_noasylum_constant_anyrepression_lower_nousopo, "electedpdc_noasylum_constant_anyrepression_lower_nousopo.tex")
electedpdc_noasylum_constant_anyrepression_upper_nousopo<-format(round(quantile(electedpdc_noasylum_anyrepression_logit_noasylum_results_nousopo$V1,probs=.95), 2))
##write(electedpdc_noasylum_constant_anyrepression_upper_nousopo, "electedpdc_noasylum_constant_anyrepression_upper_nousopo.tex")

#Physical coercion, model 1 -- no controls
pc_logit_noc_noa_nousopo<-logitCoefficients(dips73_nousopo_noasylum, c('victim', 'elected'), iterations)
functional_iterations=length(unlist(pc_logit_noc_noa_nousopo))/2

elected_noasylum_logit_noasylum_results_nousopo=as.data.frame(t(matrix(unlist(pc_logit_noc_noa_nousopo), ncol=functional_iterations)))

elected_noasylum_nousopo_reg<-glm(victim~factor(elected), data=dips73_nousopo_noasylum, family="binomial")
elected_noasylum_elected_noasylum_nousopo<-format(round(summary(elected_noasylum_nousopo_reg)$coefficients[2,1], 2))
elected_noasylum_nousopo_AIC<-format(round(summary(elected_noasylum_nousopo_reg)$aic, 2))
elected_noasylum_nousopo_obs<-dim(dips73_nousopo)[1]
##write(elected_noasylum_elected_noasylum_nousopo, "elected_noasylum_elected_noasylum_nousopo.tex")
##write(elected_noasylum_nousopo_AIC, "elected_noasylum_nousopo_AIC.tex")
##write(elected_noasylum_nousopo_obs, "elected_noasylum_nousopo_obs.tex")

elected_noasylum_lower_nousopo<-format(round(quantile(elected_noasylum_logit_noasylum_results_nousopo$V2,probs=.05), 2))
##write(elected_noasylum_lower_nousopo, "elected_noasylum_lower_nousopo.tex")
elected_noasylum_upper_nousopo<-format(round(quantile(elected_noasylum_logit_noasylum_results_nousopo$V2,probs=.95), 2))
##write(elected_noasylum_upper_nousopo, "elected_noasylum_upper_nousopo.tex")

elected_noasylum_constant_nousopo<-format(round(summary(elected_noasylum_nousopo_reg)$coefficients[1,1], 2))
##write(elected_noasylum_constant_nousopo, "elected_noasylum_constant_nousopo.tex")
elected_noasylum_constant_lower_nousopo<-format(round(quantile(elected_noasylum_logit_noasylum_results_nousopo$V1,probs=.05), 2))
##write(elected_noasylum_constant_lower_nousopo, "elected_noasylum_constant_lower_nousopo.tex")
elected_noasylum_constant_upper_nousopo<-format(round(quantile(elected_noasylum_logit_noasylum_results_nousopo$V1,probs=.95), 2))
##write(elected_noasylum_constant_upper_nousopo, "elected_noasylum_constant_upper_nousopo.tex")

##Physical coercion, model 2 --  controls
pc_logit_c_noa_nousopo<-logitCoefficients(dips73_nousopo_noasylum, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)
functional_iterations=length(unlist(pc_logit_c_noa_nousopo))/2

electedc_noasylum_logit_noasylum_results_nousopo=as.data.frame(t(matrix(unlist(pc_logit_c_noa_nousopo), ncol=functional_iterations)))

electedc_noasylum_nousopo_glm<-glm(victim~factor(elected)+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo_noasylum, family="binomial")
electedc_noasylum_elected_noasylum_nousopo<-format(round(summary(electedc_noasylum_nousopo_glm)$coefficients[2,1], 2))
##write(electedc_noasylum_elected_noasylum_nousopo, "electedc_noasylum_elected_noasylum_nousopo.tex")
electedc_noasylum_nousopo_AIC<-format(round(summary(electedc_noasylum_nousopo_glm)$aic, 2))
##write(electedc_noasylum_nousopo_AIC, "electedc_noasylum_nousopo_AIC.tex")
electedc_noasylum_lower_nousopo<-format(round(quantile(electedc_noasylum_logit_noasylum_results_nousopo$V2,probs=.05), 2))
##write(electedc_noasylum_lower_nousopo, "electedc_noasylum_lower_nousopo.tex")
electedc_noasylum_upper_nousopo<-format(round(quantile(electedc_noasylum_logit_noasylum_results_nousopo$V2,probs=.95), 2))
##write(electedc_noasylum_upper_nousopo, "electedc_noasylum_upper_nousopo.tex")

electedc_noasylum_constant_nousopo<-format(round(summary(electedc_noasylum_nousopo_glm)$coefficients[1,1], 2))
##write(electedc_noasylum_constant_nousopo, "electedc_noasylum_constant_nousopo.tex")
electedc_noasylum_constant_lower_nousopo<-format(round(quantile(electedc_noasylum_logit_noasylum_results_nousopo$V1,probs=.05), 2))
##write(electedc_noasylum_constant_lower_nousopo, "electedc_noasylum_constant_lower_nousopo.tex")
electedc_noasylum_constant_upper_nousopo<-format(round(quantile(electedc_noasylum_logit_noasylum_results_nousopo$V1,probs=.95), 2))
##write(electedc_noasylum_constant_upper_nousopo, "electedc_noasylum_constant_upper_nousopo.tex")


##Physical coercion model 3,  all covariates
pc_logit_pdc_noa_nousopo<-logitCoefficients(dips73_nousopo_noasylum, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic', 'agrupacion_factor', 'list'), iterations)
functional_iterations=length(unlist(pc_logit_pdc_noa_nousopo))/2

electedpdc_noasylum_logit_noasylum_results_nousopo=as.data.frame(t(matrix(unlist(pc_logit_pdc_noa_nousopo), ncol=functional_iterations)))

electedpdc_noasylum_nousopo_glm<-glm(victim~factor(elected)+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo_noasylum, family="binomial")
electedpdc_noasylum_elected_noasylum_nousopo<-format(round(summary(electedpdc_noasylum_nousopo_glm)$coefficients[2,1], 2))
electedpdc_noasylum_nousopo_AIC<-format(round(summary(electedpdc_noasylum_nousopo_glm)$aic, 2))
##write(electedpdc_noasylum_nousopo_AIC, "electedpdc_noasylum_nousopo_AIC.tex")
##write(electedpdc_noasylum_elected_noasylum_nousopo, "electedpdc_noasylum_elected_noasylum_nousopo.tex")
electedpdc_noasylum_lower_nousopo<-format(round(quantile(electedpdc_noasylum_logit_noasylum_results_nousopo$V2,probs=.05), 2))
##write(electedpdc_noasylum_lower_nousopo, "electedpdc_noasylum_lower_nousopo.tex")
electedpdc_noasylum_upper_nousopo<-format(round(quantile(electedpdc_noasylum_logit_noasylum_results_nousopo$V2,probs=.95), 2))
##write(electedpdc_noasylum_upper_nousopo, "electedpdc_noasylum_upper_nousopo.tex")

electedpdc_noasylum_constant_nousopo<-format(round(summary(electedpdc_noasylum_nousopo_glm)$coefficients[1,1], 2))
##write(electedpdc_noasylum_constant_nousopo, "electedpdc_noasylum_constant_nousopo.tex")
electedpdc_noasylum_constant_lower_nousopo<-format(round(quantile(electedpdc_noasylum_logit_noasylum_results_nousopo$V1,probs=.05), 2))
##write(electedpdc_noasylum_constant_lower_nousopo, "electedpdc_noasylum_constant_lower_nousopo.tex")
electedpdc_noasylum_constant_upper_nousopo<-format(round(quantile(electedpdc_noasylum_logit_noasylum_results_nousopo$V1,probs=.95), 2))
##write(electedpdc_noasylum_constant_upper_nousopo, "electedpdc_noasylum_constant_upper_nousopo.tex")

###Logistic regression, including only UP and PDC

##Any repression, model 1 -- no controls

ar_logit_noc_noa_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo_noasylum, c('anyrepression', 'vote_perc'), iterations)
functional_iterations=length(unlist(ar_logit_noc_noa_leftDC_nousopo))/2

elected_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(ar_logit_noc_noa_leftDC_nousopo), ncol=functional_iterations)))
elected_noasylum_anyrepression_leftDC_nousopo_reg<-glm(anyrepression~vote_perc, data=dips73_leftDC_nousopo_noasylum, family="binomial")
elected_noasylum_elected_noasylum_anyrepression_leftDC_nousopo<-format(round(summary(elected_noasylum_anyrepression_leftDC_nousopo_reg)$coefficients[2,1], 2))
##write(elected_noasylum_elected_noasylum_anyrepression_leftDC_nousopo, "elected_noasylum_elected_noasylum_anyrepression_leftDC_nousopo.tex")
elected_noasylum_anyrepression_leftDC_nousopo_AIC<-format(round(summary(elected_noasylum_anyrepression_leftDC_nousopo_reg)$aic, 2))
##write(elected_noasylum_anyrepression_leftDC_nousopo_AIC, "elected_noasylum_anyrepression_leftDC_nousopo_AIC.tex")

elected_noasylum_anyrepression_lower_leftDC_nousopo<-format(round(quantile(elected_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo$V2,probs=.05), 2))
##write(elected_noasylum_anyrepression_lower_leftDC_nousopo, "elected_noasylum_anyrepression_lower_leftDC_nousopo.tex")
elected_noasylum_anyrepression_upper_leftDC_nousopo<-format(round(quantile(elected_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo$V2,probs=.95), 2))
##write(elected_noasylum_anyrepression_upper_leftDC_nousopo, "elected_noasylum_anyrepression_upper_leftDC_nousopo.tex")

elected_noasylum_constant_anyrepression_leftDC_nousopo<-format(round(summary(elected_noasylum_anyrepression_leftDC_nousopo_reg)$coefficients[1,1], 2))
##write(elected_noasylum_constant_anyrepression_leftDC_nousopo, "elected_noasylum_constant_anyrepression_leftDC_nousopo.tex")
elected_noasylum_constant_anyrepression_lower_leftDC_nousopo<-format(round(quantile(elected_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo$V1,probs=.05), 2))
##write(elected_noasylum_constant_anyrepression_lower_leftDC_nousopo, "elected_noasylum_constant_anyrepression_lower_leftDC_nousopo.tex")
elected_noasylum_constant_anyrepression_upper_leftDC_nousopo<-format(round(quantile(elected_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo$V1,probs=.95), 2))
##write(elected_noasylum_constant_anyrepression_upper_leftDC_nousopo, "elected_noasylum_constant_anyrepression_upper_leftDC_nousopo.tex")

##Any repression, model 2 -- party controls
ar_logit_c_noa_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo_noasylum, c('anyrepression', 'vote_perc', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)
functional_iterations=length(unlist(ar_logit_c_noa_leftDC_nousopo))/2

electedc_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(ar_logit_c_noa_leftDC_nousopo), ncol=functional_iterations)))

electedc_noasylum_anyrepression_leftDC_nousopo_reg<-glm(anyrepression~vote_perc+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo_noasylum, family="binomial")
electedc_noasylum_elected_noasylum_anyrepression_leftDC_nousopo<-format(round(summary(electedc_noasylum_anyrepression_leftDC_nousopo_reg)$coefficients[2,1], 2))
##write(electedc_noasylum_elected_noasylum_anyrepression_leftDC_nousopo, "electedc_noasylum_elected_noasylum_anyrepression_leftDC_nousopo.tex")
electedc_noasylum_anyrepression_leftDC_nousopo_AIC<-format(round(summary(electedc_noasylum_anyrepression_leftDC_nousopo_reg)$aic, 2))
##write(electedc_noasylum_anyrepression_leftDC_nousopo_AIC, "electedc_noasylum_anyrepression_leftDC_nousopo_AIC.tex")

electedc_noasylum_anyrepression_lower_leftDC_nousopo<-format(round(quantile(electedc_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo$V2,probs=.05), 2))
##write(electedc_noasylum_anyrepression_lower_leftDC_nousopo, "electedc_noasylum_anyrepression_lower_leftDC_nousopo.tex")
electedc_noasylum_anyrepression_upper_leftDC_nousopo<-format(round(quantile(electedc_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo$V2,probs=.95), 2))
##write(electedc_noasylum_anyrepression_upper_leftDC_nousopo, "electedc_noasylum_anyrepression_upper_leftDC_nousopo.tex")

electedc_noasylum_constant_anyrepression_leftDC_nousopo<-format(round(summary(electedc_noasylum_anyrepression_leftDC_nousopo_reg)$coefficients[1,1], 2))
##write(electedc_noasylum_constant_anyrepression_leftDC_nousopo, "electedc_noasylum_constant_anyrepression_leftDC_nousopo.tex")
electedc_noasylum_constant_anyrepression_lower_leftDC_nousopo<-format(round(quantile(electedc_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo$V1,probs=.05), 2))
##write(electedc_noasylum_constant_anyrepression_lower_leftDC_nousopo, "electedc_noasylum_constant_anyrepression_lower_leftDC_nousopo.tex")
electedc_noasylum_constant_anyrepression_upper_leftDC_nousopo<-format(round(quantile(electedc_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo$V1,probs=.95), 2))
##write(electedc_noasylum_constant_anyrepression_upper_leftDC_nousopo, "electedc_noasylum_constant_anyrepression_upper_leftDC_nousopo.tex")


##Any repression, all covariates
dips73_leftDC_nousopo_noasylum$agrupacion_factor<-as.factor(dips73_leftDC_nousopo_noasylum$agrupacion)
ar_logit_pdc_noa_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo_noasylum, c('anyrepression', 'vote_perc', 'surname', 'incumbent_orpastelected', 'dynastic', 'agrupacion_factor', 'list'), iterations)
functional_iterations=length(unlist(ar_logit_pdc_noa_leftDC_nousopo))/2

electedpdc_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(ar_logit_pdc_noa_leftDC_nousopo), ncol=functional_iterations)))

electedpdc_noasylum_anyrepression_leftDC_nousopo_reg<-glm(anyrepression~vote_perc+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo_noasylum, family="binomial")
electedpdc_noasylum_elected_noasylum_anyrepression_leftDC_nousopo<-format(round(summary(electedpdc_noasylum_anyrepression_leftDC_nousopo_reg)$coefficients[2,1], 2))
##write(electedpdc_noasylum_elected_noasylum_anyrepression_leftDC_nousopo, "electedpdc_noasylum_elected_noasylum_anyrepression_leftDC_nousopo.tex")
electedpdc_noasylum_anyrepression_leftDC_nousopo_AIC<-format(round(summary(electedpdc_noasylum_anyrepression_leftDC_nousopo_reg)$aic, 2))
##write(electedpdc_noasylum_anyrepression_leftDC_nousopo_AIC, "electedpdc_noasylum_anyrepression_leftDC_nousopo_AIC.tex")

electedpdc_noasylum_anyrepression_lower_leftDC_nousopo<-format(round(quantile(electedpdc_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo$V2,probs=.05), 2))
##write(electedpdc_noasylum_anyrepression_lower_leftDC_nousopo, "electedpdc_noasylum_anyrepression_lower_leftDC_nousopo.tex")
electedpdc_noasylum_anyrepression_upper_leftDC_nousopo<-format(round(quantile(electedpdc_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo$V2,probs=.95), 2))
##write(electedpdc_noasylum_anyrepression_upper_leftDC_nousopo, "electedpdc_noasylum_anyrepression_upper_leftDC_nousopo.tex")

electedpdc_noasylum_constant_anyrepression_leftDC_nousopo<-format(round(summary(electedpdc_noasylum_anyrepression_leftDC_nousopo_reg)$coefficients[1,1], 2))
##write(electedpdc_noasylum_constant_anyrepression_leftDC_nousopo, "electedpdc_noasylum_constant_anyrepression_leftDC_nousopo.tex")
electedpdc_noasylum_constant_anyrepression_lower_leftDC_nousopo<-format(round(quantile(electedpdc_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo$V1,probs=.05), 2))
##write(electedpdc_noasylum_constant_anyrepression_lower_leftDC_nousopo, "electedpdc_noasylum_constant_anyrepression_lower_leftDC_nousopo.tex")
electedpdc_noasylum_constant_anyrepression_upper_leftDC_nousopo<-format(round(quantile(electedpdc_noasylum_anyrepression_logit_noasylum_results_leftDC_nousopo$V1,probs=.95), 2))
##write(electedpdc_noasylum_constant_anyrepression_upper_leftDC_nousopo, "electedpdc_noasylum_constant_anyrepression_upper_leftDC_nousopo.tex")

#Physical coercion, model 1 -- no controls
pc_logit_noc_noa_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo_noasylum, c('victim', 'elected'), iterations)
functional_iterations=length(unlist(pc_logit_noc_noa_leftDC_nousopo))/2

elected_noasylum_logit_noasylum_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(pc_logit_noc_noa_leftDC_nousopo), ncol=functional_iterations)))

elected_noasylum_leftDC_nousopo_reg<-glm(victim~factor(elected), data=dips73_leftDC_nousopo_noasylum, family="binomial")
elected_noasylum_elected_noasylum_leftDC_nousopo<-format(round(summary(elected_noasylum_leftDC_nousopo_reg)$coefficients[2,1], 2))
elected_noasylum_leftDC_nousopo_AIC<-format(round(summary(elected_noasylum_leftDC_nousopo_reg)$aic, 2))
elected_noasylum_leftDC_nousopo_obs<-dim(dips73_leftDC_nousopo)[1]
##write(elected_noasylum_elected_noasylum_leftDC_nousopo, "elected_noasylum_elected_noasylum_leftDC_nousopo.tex")
##write(elected_noasylum_leftDC_nousopo_AIC, "elected_noasylum_leftDC_nousopo_AIC.tex")
##write(elected_noasylum_leftDC_nousopo_obs, "elected_noasylum_leftDC_nousopo_obs.tex")

elected_noasylum_lower_leftDC_nousopo<-format(round(quantile(elected_noasylum_logit_noasylum_results_leftDC_nousopo$V2,probs=.05), 2))
##write(elected_noasylum_lower_leftDC_nousopo, "elected_noasylum_lower_leftDC_nousopo.tex")
elected_noasylum_upper_leftDC_nousopo<-format(round(quantile(elected_noasylum_logit_noasylum_results_leftDC_nousopo$V2,probs=.95), 2))
##write(elected_noasylum_upper_leftDC_nousopo, "elected_noasylum_upper_leftDC_nousopo.tex")

elected_noasylum_constant_leftDC_nousopo<-format(round(summary(elected_noasylum_leftDC_nousopo_reg)$coefficients[1,1], 2))
##write(elected_noasylum_constant_leftDC_nousopo, "elected_noasylum_constant_leftDC_nousopo.tex")
elected_noasylum_constant_lower_leftDC_nousopo<-format(round(quantile(elected_noasylum_logit_noasylum_results_leftDC_nousopo$V1,probs=.05), 2))
##write(elected_noasylum_constant_lower_leftDC_nousopo, "elected_noasylum_constant_lower_leftDC_nousopo.tex")
elected_noasylum_constant_upper_leftDC_nousopo<-format(round(quantile(elected_noasylum_logit_noasylum_results_leftDC_nousopo$V1,probs=.95), 2))
##write(elected_noasylum_constant_upper_leftDC_nousopo, "elected_noasylum_constant_upper_leftDC_nousopo.tex")

##Physical coercion, model 2 --  controls
pc_logit_c_noa_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo_noasylum, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)
functional_iterations=length(unlist(pc_logit_c_noa_leftDC_nousopo))/2

electedc_noasylum_logit_noasylum_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(pc_logit_c_noa_leftDC_nousopo), ncol=functional_iterations)))

electedc_noasylum_leftDC_nousopo_glm<-glm(victim~factor(elected)+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo_noasylum, family="binomial")
electedc_noasylum_elected_noasylum_leftDC_nousopo<-format(round(summary(electedc_noasylum_leftDC_nousopo_glm)$coefficients[2,1], 2))
##write(electedc_noasylum_elected_noasylum_leftDC_nousopo, "electedc_noasylum_elected_noasylum_leftDC_nousopo.tex")
electedc_noasylum_leftDC_nousopo_AIC<-format(round(summary(electedc_noasylum_leftDC_nousopo_glm)$aic, 2))
##write(electedc_noasylum_leftDC_nousopo_AIC, "electedc_noasylum_leftDC_nousopo_AIC.tex")
electedc_noasylum_lower_leftDC_nousopo<-format(round(quantile(electedc_noasylum_logit_noasylum_results_leftDC_nousopo$V2,probs=.05), 2))
##write(electedc_noasylum_lower_leftDC_nousopo, "electedc_noasylum_lower_leftDC_nousopo.tex")
electedc_noasylum_upper_leftDC_nousopo<-format(round(quantile(electedc_noasylum_logit_noasylum_results_leftDC_nousopo$V2,probs=.95), 2))
##write(electedc_noasylum_upper_leftDC_nousopo, "electedc_noasylum_upper_leftDC_nousopo.tex")

electedc_noasylum_constant_leftDC_nousopo<-format(round(summary(electedc_noasylum_leftDC_nousopo_glm)$coefficients[1,1], 2))
##write(electedc_noasylum_constant_leftDC_nousopo, "electedc_noasylum_constant_leftDC_nousopo.tex")
electedc_noasylum_constant_lower_leftDC_nousopo<-format(round(quantile(electedc_noasylum_logit_noasylum_results_leftDC_nousopo$V1,probs=.05), 2))
##write(electedc_noasylum_constant_lower_leftDC_nousopo, "electedc_noasylum_constant_lower_leftDC_nousopo.tex")
electedc_noasylum_constant_upper_leftDC_nousopo<-format(round(quantile(electedc_noasylum_logit_noasylum_results_leftDC_nousopo$V1,probs=.95), 2))
##write(electedc_noasylum_constant_upper_leftDC_nousopo, "electedc_noasylum_constant_upper_leftDC_nousopo.tex")


##Physical coercion, model 3 -- all covariates
pc_logit_pdc_noa_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo_noasylum, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic', 'agrupacion_factor', 'list'), iterations)
functional_iterations=length(unlist(pc_logit_pdc_noa_leftDC_nousopo))/2

electedpdc_noasylum_logit_noasylum_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(pc_logit_pdc_noa_leftDC_nousopo), ncol=functional_iterations)))

electedpdc_noasylum_leftDC_nousopo_glm<-glm(victim~factor(elected)+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo_noasylum, family="binomial")
electedpdc_noasylum_elected_noasylum_leftDC_nousopo<-format(round(summary(electedpdc_noasylum_leftDC_nousopo_glm)$coefficients[2,1], 2))
electedpdc_noasylum_leftDC_nousopo_AIC<-format(round(summary(electedpdc_noasylum_leftDC_nousopo_glm)$aic, 2))
##write(electedpdc_noasylum_leftDC_nousopo_AIC, "electedpdc_noasylum_leftDC_nousopo_AIC.tex")
##write(electedpdc_noasylum_elected_noasylum_leftDC_nousopo, "electedpdc_noasylum_elected_noasylum_leftDC_nousopo.tex")
electedpdc_noasylum_lower_leftDC_nousopo<-format(round(quantile(electedpdc_noasylum_logit_noasylum_results_leftDC_nousopo$V2,probs=.05), 2))
##write(electedpdc_noasylum_lower_leftDC_nousopo, "electedpdc_noasylum_lower_leftDC_nousopo.tex")
electedpdc_noasylum_upper_leftDC_nousopo<-format(round(quantile(electedpdc_noasylum_logit_noasylum_results_leftDC_nousopo$V2,probs=.95), 2))
##write(electedpdc_noasylum_upper_leftDC_nousopo, "electedpdc_noasylum_upper_leftDC_nousopo.tex")

electedpdc_noasylum_constant_leftDC_nousopo<-format(round(summary(electedpdc_noasylum_leftDC_nousopo_glm)$coefficients[1,1], 2))
##write(electedpdc_noasylum_constant_leftDC_nousopo, "electedpdc_noasylum_constant_leftDC_nousopo.tex")
electedpdc_noasylum_constant_lower_leftDC_nousopo<-format(round(quantile(electedpdc_noasylum_logit_noasylum_results_leftDC_nousopo$V1,probs=.05), 2))
##write(electedpdc_noasylum_constant_lower_leftDC_nousopo, "electedpdc_noasylum_constant_lower_leftDC_nousopo.tex")
electedpdc_noasylum_constant_upper_leftDC_nousopo<-format(round(quantile(electedpdc_noasylum_logit_noasylum_results_leftDC_nousopo$V1,probs=.95), 2))
##write(electedpdc_noasylum_constant_upper_leftDC_nousopo, "electedpdc_noasylum_constant_upper_leftDC_nousopo.tex")



##################################################################
############Figure C2: Close diff-in-means, excluding asylum######
##################################################################


dips73_noasylum$levels<-ifelse(dips73_noasylum$quotient_diff<quantile(dips73_noasylum$quotient_diff[dips73_noasylum$elected==0], .5), 0, 1)
dips73_noasylum$levels<-ifelse(dips73_noasylum$quotient_diff>quantile(dips73_noasylum$quotient_diff[dips73_noasylum$elected==1], .5), 3, dips73_noasylum$levels)
dips73_noasylum$levels<-ifelse(dips73_noasylum$elected==1&dips73_noasylum$levels==1, 2, dips73_noasylum$levels)

dips73_nousopo_noasylum$levels<-ifelse(dips73_nousopo_noasylum$quotient_diff<quantile(dips73_nousopo_noasylum$quotient_diff[dips73_nousopo_noasylum$elected==0], .5), 0, 1)
dips73_nousopo_noasylum$levels<-ifelse(dips73_nousopo_noasylum$quotient_diff>quantile(dips73_nousopo_noasylum$quotient_diff[dips73_nousopo_noasylum$elected==1], .5), 3, dips73_nousopo_noasylum$levels)
dips73_nousopo_noasylum$levels<-ifelse(dips73_nousopo_noasylum$elected==1&dips73_nousopo_noasylum$levels==1, 2, dips73_nousopo_noasylum$levels)

dips73_leftDC_nousopo_noasylum$levels<-ifelse(dips73_leftDC_nousopo_noasylum$quotient_diff<quantile(dips73_leftDC_nousopo_noasylum$quotient_diff[dips73_leftDC_nousopo_noasylum$elected==0], .5), 0, 1)
dips73_leftDC_nousopo_noasylum$levels<-ifelse(dips73_leftDC_nousopo_noasylum$quotient_diff>quantile(dips73_leftDC_nousopo_noasylum$quotient_diff[dips73_leftDC_nousopo_noasylum$elected==1], .5), 3, dips73_leftDC_nousopo_noasylum$levels)
dips73_leftDC_nousopo_noasylum$levels<-ifelse(dips73_leftDC_nousopo_noasylum$elected==1&dips73_leftDC_nousopo_noasylum$levels==1, 2, dips73_leftDC_nousopo_noasylum$levels)


anyrepression_noasylum_plot<-whiskerPlotLevels(dips73_noasylum, dips73_noasylum$anyrepression, " ", "Overall repression mean", "All candidates")
anyrepression_noasylum_leftDC_plot<-whiskerPlotLevels(dips73_leftDC_nousopo_noasylum, dips73_leftDC_nousopo_noasylum$anyrepression, " ", " ", "UP and PDC Candidates")

physicalcoercion_noasylum_plot<-whiskerPlotLevels(dips73_noasylum, dips73_noasylum$victim, "Group", "Physical coercion mean", " ")
physicalcoercion_noasylum_leftDC_plot<-whiskerPlotLevels(dips73_leftDC_nousopo_noasylum, dips73_leftDC_nousopo_noasylum$victim, "Group", " ", " ")

#pdf("whisker_close_plots_noasylum.pdf", width=10, height=8, family="Times New Roman")
multiplot(anyrepression_noasylum_plot, physicalcoercion_noasylum_plot, anyrepression_noasylum_leftDC_plot, physicalcoercion_noasylum_leftDC_plot, cols=2)
#dev.off()


############################################################
######Table C4: RDD LLR, excluding asylum seekers###########
############################################################

noasylum_anyrepression<-RDestimate(anyrepression~quotient_diff, data=dips73_noasylum, cluster=dips73_noasylum$Distrito)
##write(format(round(noasylum_anyrepression$est[1], 2)), "late_anyrepression_noasylum.tex")
##write(format(round(noasylum_anyrepression$est[2], 2)), "half_bw_anyrepression_noasylum.tex")
##write(format(round(noasylum_anyrepression$est[3], 2)), "double_bw_anyrepression_noasylum.tex")

##write(format(round(noasylum_anyrepression$obs[1], 2)), "obs_late_anyrepression_noasylum.tex")
##write(format(round(noasylum_anyrepression$obs[2], 2)), "obs_half_late_anyrepression_noasylum.tex")
##write(format(round(noasylum_anyrepression$obs[3], 2)), "obs_double_late_anyrepression_noasylum.tex")

##write(format(round(noasylum_anyrepression$bw[1], 2)), "bw_late_anyrepression_noasylum.tex")
##write(format(round(noasylum_anyrepression$bw[2], 2)), "bw_half_late_anyrepression_noasylum.tex")
##write(format(round(noasylum_anyrepression$bw[3], 2)), "bw_double_late_anyrepression_noasylum.tex")

noasylum_anyrepression_p<-RDestimate(anyrepression~quotient_diff|Partido, data=dips73_noasylum, cluster=dips73_noasylum$Distrito)

##write(format(round(noasylum_anyrepression_p$est[1], 2)), "latep_anyrepression_noasylum.tex")
##write(format(round(noasylum_anyrepression_p$est[2], 2)), "halfp_anyrepression_noasylum.tex")
##write(format(round(noasylum_anyrepression_p$est[3], 2)), "doublep_anyrepression_noasylum.tex")

LLR_anyrepression_noasylum<-bootstrapLLR(noasylum_anyrepression_data, iterations)
late_lower_anyrepression_noasylum<-format(round(quantile(LLR_anyrepression_noasylum$LLR, probs=.05), 2))
late_upper_anyrepression_noasylum<-format(round(quantile(LLR_anyrepression_noasylum$LLR, probs=.95), 2))
##write(late_lower_anyrepression_noasylum, "late_lower_anyrepression_noasylum.tex")
##write(late_upper_anyrepression_noasylum, "late_upper_anyrepression_noasylum.tex")

late_lower_anyrepression_noasylum_95<-format(round(quantile(LLR_anyrepression_noasylum$LLR, probs=.025), 2))
late_upper_anyrepression_noasylum_95<-format(round(quantile(LLR_anyrepression_noasylum$LLR, probs=.975), 2))
##write(late_lower_anyrepression_noasylum_95, "late_lower_anyrepression_noasylum_95.tex")
##write(late_upper_anyrepression_noasylum_95, "late_upper_anyrepression_noasylum_95.tex")

late_half_lower_anyrepression_noasylum<-format(round(quantile(LLR_anyrepression_noasylum$LLRhalf, probs=.05), 2))
late_half_upper_anyrepression_noasylum<-format(round(quantile(LLR_anyrepression_noasylum$LLRhalf, probs=.95), 2))
##write(late_half_lower_anyrepression_noasylum, "late_half_lower_anyrepression_noasylum.tex")
##write(late_half_upper_anyrepression_noasylum, "late_half_upper_anyrepression_noasylum.tex")

late_half_lower_anyrepression_noasylum_95<-format(round(quantile(LLR_anyrepression_noasylum$LLRhalf, probs=.025), 2))
late_half_upper_anyrepression_noasylum_95<-format(round(quantile(LLR_anyrepression_noasylum$LLRhalf, probs=.975), 2))
##write(late_half_lower_anyrepression_noasylum_95, "late_half_lower_anyrepression_noasylum_95.tex")
##write(late_half_upper_anyrepression_noasylum_95, "late_half_upper_anyrepression_noasylum_95.tex")

late_2_lower_anyrepression_noasylum<-format(round(quantile(LLR_anyrepression_noasylum$LLR2, probs=.05), 2))
late_2_upper_anyrepression_noasylum<-format(round(quantile(LLR_anyrepression_noasylum$LLR2, probs=.95), 2))
##write(late_2_lower_anyrepression_noasylum, "late_2_lower_anyrepression_noasylum.tex")
##write(late_2_upper_anyrepression_noasylum, "late_2_upper_anyrepression_noasylum.tex")

late_2_lower_anyrepression_noasylum_95<-format(round(quantile(LLR_anyrepression_noasylum$LLR2, probs=.025), 2))
late_2_upper_anyrepression_noasylum_95<-format(round(quantile(LLR_anyrepression_noasylum$LLR2, probs=.975), 2))
##write(late_2_lower_anyrepression_noasylum_95, "late_2_lower_anyrepression_noasylum_95.tex")
##write(late_2_upper_anyrepression_noasylum_95, "late_2_upper_anyrepression_noasylum_95.tex")

late_party_lower_anyrepression_noasylum<-format(round(quantile(LLR_anyrepression_noasylum$LLRparty, probs=.05), 2))
late_party_upper_anyrepression_noasylum<-format(round(quantile(LLR_anyrepression_noasylum$LLRparty, probs=.95), 2))
##write(late_party_lower_anyrepression_noasylum, "late_party_lower_anyrepression_noasylum.tex")
##write(late_party_upper_anyrepression_noasylum, "late_party_upper_anyrepression_noasylum.tex")

late_party_lower_anyrepression_noasylum_95<-format(round(quantile(LLR_anyrepression_noasylum$LLRparty, probs=.025), 2))
late_party_upper_anyrepression_noasylum_95<-format(round(quantile(LLR_anyrepression_noasylum$LLRparty, probs=.975), 2))
##write(late_party_lower_anyrepression_noasylum_95, "late_party_lower_anyrepression_noasylum_95.tex")
##write(late_party_upper_anyrepression_noasylum_95, "late_party_upper_anyrepression_noasylum_95.tex")

late_party_half_lower_anyrepression_noasylum<-format(round(quantile(LLR_anyrepression_noasylum$LLRpartyhalf, probs=.05), 2))
late_party_half_upper_anyrepression_noasylum<-format(round(quantile(LLR_anyrepression_noasylum$LLRpartyhalf, probs=.95), 2))
##write(late_party_half_lower_anyrepression_noasylum, "late_party_half_lower_anyrepression_noasylum.tex")
##write(late_party_half_upper_anyrepression_noasylum, "late_party_half_upper_anyrepression_noasylum.tex")

late_party_half_lower_anyrepression_noasylum_95<-format(round(quantile(LLR_anyrepression_noasylum$LLRpartyhalf, probs=.025), 2))
late_party_half_upper_anyrepression_noasylum_95<-format(round(quantile(LLR_anyrepression_noasylum$LLRpartyhalf, probs=.975), 2))
##write(late_party_half_lower_anyrepression_noasylum_95, "late_party_half_lower_anyrepression_noasylum_95.tex")
##write(late_party_half_upper_anyrepression_noasylum_95, "late_party_half_upper_anyrepression_noasylum_95.tex")

late_party_2_lower_anyrepression_noasylum<-format(round(quantile(LLR_anyrepression_noasylum$LLRparty2, probs=.05), 2))
late_party_2_upper_anyrepression_noasylum<-format(round(quantile(LLR_anyrepression_noasylum$LLRparty2, probs=.95), 2))
##write(late_party_2_lower_anyrepression_noasylum, "late_party_2_lower_anyrepression_noasylum.tex")
##write(late_party_2_upper_anyrepression_noasylum, "late_party_2_upper_anyrepression_noasylum.tex")

late_party_2_lower_anyrepression_noasylum_95<-format(round(quantile(LLR_anyrepression_noasylum$LLRparty2, probs=.05), 2))
late_party_2_upper_anyrepression_noasylum_95<-format(round(quantile(LLR_anyrepression_noasylum$LLRparty2, probs=.95), 2))
##write(late_party_2_lower_anyrepression_noasylum_95, "late_party_2_lower_anyrepression_noasylum_95.tex")
##write(late_party_2_upper_anyrepression_noasylum_95, "late_party_2_upper_anyrepression_noasylum_95.tex")


##Physical coercion
noasylum_physicalcoercion<-RDestimate(victim~quotient_diff, data=dips73_noasylum, cluster=dips73_noasylum$Distrito)
##write(format(round(noasylum_physicalcoercion$est[1], 2)), "late_physicalcoercion_noasylum.tex")
##write(format(round(noasylum_physicalcoercion$est[2], 2)), "half_bw_physicalcoercion_noasylum.tex")
##write(format(round(noasylum_physicalcoercion$est[3], 2)), "double_bw_physicalcoercion_noasylum.tex")

##write(format(round(noasylum_physicalcoercion$obs[1], 2)), "obs_late_physicalcoercion_noasylum.tex")
##write(format(round(noasylum_physicalcoercion$obs[2], 2)), "obs_half_late_physicalcoercion_noasylum.tex")
##write(format(round(noasylum_physicalcoercion$obs[3], 2)), "obs_double_late_physicalcoercion_noasylum.tex")

##write(format(round(noasylum_physicalcoercion$bw[1], 2)), "bw_late_physicalcoercion_noasylum.tex")
##write(format(round(noasylum_physicalcoercion$bw[2], 2)), "bw_half_late_physicalcoercion_noasylum.tex")
##write(format(round(noasylum_physicalcoercion$bw[3], 2)), "bw_double_late_physicalcoercion_noasylum.tex")

noasylum_physicalcoercion_p<-RDestimate(victim~quotient_diff|Partido, data=dips73_noasylum, cluster=dips73_noasylum$Distrito)
##write(format(round(noasylum_physicalcoercion_p$est[1], 2)), "latep_physicalcoercion_noasylum.tex")
##write(format(round(noasylum_physicalcoercion_p$est[2], 2)), "halfp_physicalcoercion_noasylum.tex")
##write(format(round(noasylum_physicalcoercion_p$est[3], 2)), "doublep_physicalcoercion_noasylum.tex")

LLR_physicalcoercion_noasylum<-bootstrapLLR(noasylum_physicalcoercion_data, iterations)
late_lower_physicalcoercion_noasylum<-format(round(quantile(LLR_physicalcoercion_noasylum$LLR, probs=.05), 2))
late_upper_physicalcoercion_noasylum<-format(round(quantile(LLR_physicalcoercion_noasylum$LLR, probs=.95), 2))
##write(late_lower_physicalcoercion_noasylum, "late_lower_physicalcoercion_noasylum.tex")
##write(late_upper_physicalcoercion_noasylum, "late_upper_physicalcoercion_noasylum.tex")

late_lower_physicalcoercion_noasylum_95<-format(round(quantile(LLR_physicalcoercion_noasylum$LLR, probs=.025), 2))
late_upper_physicalcoercion_noasylum_95<-format(round(quantile(LLR_physicalcoercion_noasylum$LLR, probs=.975), 2))
##write(late_lower_physicalcoercion_noasylum_95, "late_lower_physicalcoercion_noasylum_95.tex")
##write(late_upper_physicalcoercion_noasylum_95, "late_upper_physicalcoercion_noasylum_95.tex")

late_half_lower_physicalcoercion_noasylum<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRhalf, probs=.05), 2))
late_half_upper_physicalcoercion_noasylum<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRhalf, probs=.95), 2))
##write(late_half_lower_physicalcoercion_noasylum, "late_half_lower_physicalcoercion_noasylum.tex")
##write(late_half_upper_physicalcoercion_noasylum, "late_half_upper_physicalcoercion_noasylum.tex")

late_half_lower_physicalcoercion_noasylum_95<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRhalf, probs=.025), 2))
late_half_upper_physicalcoercion_noasylum_95<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRhalf, probs=.975), 2))
##write(late_half_lower_physicalcoercion_noasylum_95, "late_half_lower_physicalcoercion_noasylum_95.tex")
##write(late_half_upper_physicalcoercion_noasylum_95, "late_half_upper_physicalcoercion_noasylum_95.tex")

late_2_lower_physicalcoercion_noasylum<-format(round(quantile(LLR_physicalcoercion_noasylum$LLR2, probs=.05), 2))
late_2_upper_physicalcoercion_noasylum<-format(round(quantile(LLR_physicalcoercion_noasylum$LLR2, probs=.95), 2))
##write(late_2_lower_physicalcoercion_noasylum, "late_2_lower_physicalcoercion_noasylum.tex")
##write(late_2_upper_physicalcoercion_noasylum, "late_2_upper_physicalcoercion_noasylum.tex")

late_2_lower_physicalcoercion_noasylum_95<-format(round(quantile(LLR_physicalcoercion_noasylum$LLR2, probs=.025), 2))
late_2_upper_physicalcoercion_noasylum_95<-format(round(quantile(LLR_physicalcoercion_noasylum$LLR2, probs=.975), 2))
##write(late_2_lower_physicalcoercion_noasylum_95, "late_2_lower_physicalcoercion_noasylum_95.tex")
##write(late_2_upper_physicalcoercion_noasylum_95, "late_2_upper_physicalcoercion_noasylum_95.tex")

late_party_lower_physicalcoercion_noasylum<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRparty, probs=.05), 2))
late_party_upper_physicalcoercion_noasylum<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRparty, probs=.95), 2))
##write(late_party_lower_physicalcoercion_noasylum, "late_party_lower_physicalcoercion_noasylum.tex")
##write(late_party_upper_physicalcoercion_noasylum, "late_party_upper_physicalcoercion_noasylum.tex")

late_party_lower_physicalcoercion_noasylum_95<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRparty, probs=.025), 2))
late_party_upper_physicalcoercion_noasylum_95<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRparty, probs=.975), 2))
##write(late_party_lower_physicalcoercion_noasylum_95, "late_party_lower_physicalcoercion_noasylum_95.tex")
##write(late_party_upper_physicalcoercion_noasylum_95, "late_party_upper_physicalcoercion_noasylum_95.tex")

late_party_half_lower_physicalcoercion_noasylum<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRpartyhalf, probs=.05), 2))
late_party_half_upper_physicalcoercion_noasylum<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRpartyhalf, probs=.95), 2))
##write(late_party_half_lower_physicalcoercion_noasylum, "late_party_half_lower_physicalcoercion_noasylum.tex")
##write(late_party_half_upper_physicalcoercion_noasylum, "late_party_half_upper_physicalcoercion_noasylum.tex")

late_party_half_lower_physicalcoercion_noasylum_95<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRpartyhalf, probs=.025), 2))
late_party_half_upper_physicalcoercion_noasylum_95<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRpartyhalf, probs=.975), 2))
##write(late_party_half_lower_physicalcoercion_noasylum_95, "late_party_half_lower_physicalcoercion_noasylum_95.tex")
##write(late_party_half_upper_physicalcoercion_noasylum_95, "late_party_half_upper_physicalcoercion_noasylum_95.tex")

late_party_2_lower_physicalcoercion_noasylum<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRparty2, probs=.05), 2))
late_party_2_upper_physicalcoercion_noasylum<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRparty2, probs=.95), 2))
##write(late_party_2_lower_physicalcoercion_noasylum, "late_party_2_lower_physicalcoercion_noasylum.tex")
##write(late_party_2_upper_physicalcoercion_noasylum, "late_party_2_upper_physicalcoercion_noasylum.tex")

late_party_2_lower_physicalcoercion_noasylum_95<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRparty2, probs=.025), 2))
late_party_2_upper_physicalcoercion_noasylum_95<-format(round(quantile(LLR_physicalcoercion_noasylum$LLRparty2, probs=.975), 2))
##write(late_party_2_lower_physicalcoercion_noasylum_95, "late_party_2_lower_physicalcoercion_noasylum_95.tex")
##write(late_party_2_upper_physicalcoercion_noasylum_95, "late_party_2_upper_physicalcoercion_noasylum_95.tex")



###################################################
################Figure C3: T-tests, 1969##########
###################################################

ttest_anyrepression_69<-ttest_electedvunelected(dips69$anyrepression, dips69)
ttest_anyrepression_69$type<-'3. anyrepression_all'

ttest_anyrepression_nousopo_69<-ttest_electedvunelected(dips69_nounelected$anyrepression, dips69_nounelected)
ttest_anyrepression_nousopo_69$type<-'2. anyrepression_nousopo'

ttest_anyrepression_leftDC_69<-ttest_electedvunelected(dips69_leftDC_nounelected$anyrepression, dips69_leftDC_nounelected)
ttest_anyrepression_leftDC_69$type<-'1. anyrepression_left'

anyrepression_ttest_df_69<-rbind(ttest_anyrepression_69, ttest_anyrepression_nousopo_69, ttest_anyrepression_leftDC_69)
colnames(anyrepression_ttest_df_69)<-c('elected', 'unelected', 'difference', 'lower', 'upper', 'CI', 'type')

anyrepression_ttest_plot_69<-ggplot(anyrepression_ttest_df_69, aes(x=type, y=as.numeric(as.character(difference)), group=CI, linetype=CI))+
  geom_point()+
  theme_bw()+geom_pointrange(size=.5, ymin=as.numeric(as.character(anyrepression_ttest_df_69$lower)), 
                             ymax=as.numeric(as.character(anyrepression_ttest_df_69$upper)))+
  ylim(-5, 25)+scale_linetype_manual(values=c('solid', 'dotted'),
                                     guide = guide_legend(override.aes = list(
                                       linetype = c("solid", "dotted"),
                                       shape=c(NA, NA)) ))+
  theme(legend.position="bottom",  axis.title=element_text(size=16),
        text=element_text(family="Times New Roman", size=16),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+
  coord_flip() +
  geom_hline(aes(yintercept=0), lty=4) +
  ylab("") +
  xlab("")+scale_x_discrete(labels=c('UP and PDC', 'Excluding unelected', 'All Candidates'))
#ggsave('anyrepression_ttest_plot_69.pdf', plot=anyrepression_ttest_plot_69, height=4, width=5)



ttest_victim_69<-ttest_electedvunelected(dips69$victim, dips69)
ttest_victim_69$type<-'3. victim_all'

ttest_victim_nousopo_69<-ttest_electedvunelected(dips69_nounelected$victim, dips69_nounelected)
ttest_victim_nousopo_69$type<-'2. victim_nousopo'

ttest_victim_leftDC_69<-ttest_electedvunelected(dips69_leftDC_nounelected$victim, dips69_leftDC_nounelected)
ttest_victim_leftDC_69$type<-'1. victim_left'

victim_ttest_df_69<-rbind(ttest_victim_69, ttest_victim_nousopo_69, ttest_victim_leftDC_69)
colnames(victim_ttest_df_69)<-c('elected', 'unelected', 'difference', 'lower', 'upper', 'CI', 'type')

physicalcoercion_ttest_plot_69<-ggplot(victim_ttest_df_69, aes(x=type, y=as.numeric(as.character(difference)), group=CI, linetype=CI))+
  geom_point()+
  theme_bw()+geom_pointrange(size=.5, ymin=as.numeric(as.character(victim_ttest_df_69$lower)), 
                             ymax=as.numeric(as.character(victim_ttest_df_69$upper)))+
  ylim(-15, 5)+scale_linetype_manual(values=c('solid', 'dotted'),
                                     guide = guide_legend(override.aes = list(
                                       linetype = c("solid", "dotted"),
                                       shape=c(NA, NA)) ))+
  theme(legend.position="bottom",  axis.title=element_text(size=16),
        text=element_text(family="Times New Roman", size=16),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+
  coord_flip() +
  geom_hline(aes(yintercept=0), lty=4) +
  scale_x_discrete(labels=c('UP and PDC', 'Excluding unelected', 'All Candidates'))

#ggsave('physicalcoercion_ttest_plot_69.pdf', plot=physicalcoercion_ttest_plot_69, height=4, width=5)



#################################################################
###############Tables C5 & C6: Logit, 1969 ######################
#################################################################

##no controls
dips69_noin<-subset(dips69, Partido!="IN")

dips69$district<-as.numeric(dips69$Distrito)
dips69_noin$district<-as.numeric(dips69_noin$Distrito)
dips69_nounelected$district<-as.numeric(dips69_nounelected$Distrito)
dips69_leftDC_nounelected$district<-as.numeric(dips69_leftDC_nounelected$Distrito)

ar_logit_noc_69<-logitCoefficients(dips69_noin, c('anyrepression', 'vote_perc'), iterations)
functional_iterations=length(unlist(ar_logit_noc_69))/2

elected_anyrepression_results_69=as.data.frame(t(matrix(unlist(ar_logit_noc_69), ncol=functional_iterations)))
elected_anyrepression_69_reg<-glm(anyrepression~vote_perc, data=dips69_noin, family="binomial")
elected_elected_anyrepression_69<-format(round(summary(elected_anyrepression_69_reg)$coefficients[2,1], 2))
##write(elected_elected_anyrepression_69, "elected_elected_anyrepression_69.tex")
elected_anyrepression_69_AIC<-format(round(summary(elected_anyrepression_69_reg)$aic, 2))
##write(elected_anyrepression_69_AIC, "elected_anyrepression_69_AIC.tex")

elected_anyrepression_lower_69<-format(round(quantile(elected_anyrepression_results_69$V2,probs=.05), 2))
##write(elected_anyrepression_lower_69, "elected_anyrepression_lower_69.tex")
elected_anyrepression_upper_69<-format(round(quantile(elected_anyrepression_results_69$V2,probs=.95), 2))
##write(elected_anyrepression_upper_69, "elected_anyrepression_upper_69.tex")

elected_constant_anyrepression_69<-format(round(summary(elected_anyrepression_69_reg)$coefficients[1,1], 2))
##write(elected_constant_anyrepression_69, "elected_constant_anyrepression_69.tex")
elected_constant_anyrepression_lower_69<-format(round(quantile(elected_anyrepression_results_69$V1,probs=.05), 2))
##write(elected_constant_anyrepression_lower_69, "elected_constant_anyrepression_lower_69.tex")
elected_constant_anyrepression_upper_69<-format(round(quantile(elected_anyrepression_results_69$V1,probs=.95), 2))
##write(elected_constant_anyrepression_upper_69, "elected_constant_anyrepression_upper_69.tex")

## controls
ar_logit_c_69<-logitCoefficients(dips69_noin, c('anyrepression', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(ar_logit_c_69))/2

electedc_anyrepression_results_69=as.data.frame(t(matrix(unlist(ar_logit_c_69), ncol=functional_iterations)))

electedc_anyrepression_69_reg<-glm(anyrepression~vote_perc+surname+dynastic+incumbent_orpastelected, data=dips69_nounelected, family="binomial")
electedc_elected_anyrepression_69<-format(round(summary(electedc_anyrepression_69_reg)$coefficients[2,1], 2))
##write(electedc_elected_anyrepression_69, "electedc_elected_anyrepression_69.tex")
electedc_anyrepression_69_AIC<-format(round(summary(electedc_anyrepression_69_reg)$aic, 2))
##write(electedc_anyrepression_69_AIC, "electedc_anyrepression_69_AIC.tex")

electedc_anyrepression_lower_69<-format(round(quantile(electedc_anyrepression_results_69$V2,probs=.05), 2))
##write(electedc_anyrepression_lower_69, "electedc_anyrepression_lower_69.tex")
electedc_anyrepression_upper_69<-format(round(quantile(electedc_anyrepression_results_69$V2,probs=.95), 2))
##write(electedc_anyrepression_upper_69, "electedc_anyrepression_upper_69.tex")

electedc_constant_anyrepression_69<-format(round(summary(electedc_anyrepression_69_reg)$coefficients[1,1], 2))
##write(electedc_constant_anyrepression_69, "electedc_constant_anyrepression_69.tex")
electedc_constant_anyrepression_lower_69<-format(round(quantile(electedc_anyrepression_results_69$V1,probs=.05), 2))
##write(electedc_constant_anyrepression_lower_69, "electedc_constant_anyrepression_lower_69.tex")
electedc_constant_anyrepression_upper_69<-format(round(quantile(electedc_anyrepression_results_69$V1,probs=.95), 2))
##write(electedc_constant_anyrepression_upper_69, "electedc_constant_anyrepression_upper_69.tex")


###Party + district + covariates
dips69_noin$agrupacion_factor<-as.factor(dips69_noin$agrupacion)

ar_logit_pdc_69<-logitCoefficients(dips69_noin, c('anyrepression', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected', "Partido", 'agrupacion_factor'), iterations)
functional_iterations=length(unlist(ar_logit_pdc_69))/2

electedpdc_anyrepression_results_69=as.data.frame(t(matrix(unlist(ar_logit_pdc_69), ncol=functional_iterations)))

electedpdc_anyrepression_69_reg<-glm(anyrepression~vote_perc+Partido+factor(agrupacion)+surname+dynastic+incumbent_orpastelected, data=dips69_noin, family="binomial")
electedpdc_elected_anyrepression_69<-format(round(summary(electedpdc_anyrepression_69_reg)$coefficients[2,1], 2))
##write(electedpdc_elected_anyrepression_69, "electedpdc_elected_anyrepression_69.tex")
electedpdc_anyrepression_69_AIC<-format(round(summary(electedpdc_anyrepression_69_reg)$aic, 2))
##write(electedpdc_anyrepression_69_AIC, "electedpdc_anyrepression_69_AIC.tex")

electedpdc_anyrepression_lower_69<-format(round(quantile(electedpdc_anyrepression_results_69$V2,probs=.05), 2))
##write(electedpdc_anyrepression_lower_69, "electedpdc_anyrepression_lower_69.tex")
electedpdc_anyrepression_upper_69<-format(round(quantile(electedpdc_anyrepression_results_69$V2,probs=.95), 2))
##write(electedpdc_anyrepression_upper_69, "electedpdc_anyrepression_upper_69.tex")

electedpdc_constant_anyrepression_69<-format(round(summary(electedpdc_anyrepression_69_reg)$coefficients[1,1], 2))
##write(electedpdc_constant_anyrepression_69, "electedpdc_constant_anyrepression_69.tex")
electedpdc_constant_anyrepression_lower_69<-format(round(quantile(electedpdc_anyrepression_results_69$V1,probs=.05), 2))
##write(electedpdc_constant_anyrepression_lower_69, "electedpdc_constant_anyrepression_lower_69.tex")
electedpdc_constant_anyrepression_upper_69<-format(round(quantile(electedpdc_anyrepression_results_69$V1,probs=.95), 2))
##write(electedpdc_constant_anyrepression_upper_69, "electedpdc_constant_anyrepression_upper_69.tex")

########Physical coercion

#No controls
pc_logit_noc_69<-logitCoefficients(dips69_noin, c('victim', 'elected'), iterations)
functional_iterations=length(unlist(pc_logit_noc_69))/2

elected_results_69=as.data.frame(t(matrix(unlist(pc_logit_noc_69), ncol=functional_iterations)))

elected_69_reg<-glm(victim~factor(elected), data=dips69_noin, family="binomial")
elected_elected_69<-format(round(summary(elected_69_reg)$coefficients[2,1], 2))
elected_69_AIC<-format(round(summary(elected_69_reg)$aic, 2))
elected_69_obs<-dim(dips69_noin)[1]
##write(elected_elected_69, "elected_elected_69.tex")
##write(elected_69_AIC, "elected_69_AIC.tex")
##write(elected_69_obs, "elected_69_obs.tex")

elected_lower_69<-format(round(quantile(elected_results_69$V2,probs=.05), 2))
##write(elected_lower_69, "elected_lower_69.tex")
elected_upper_69<-format(round(quantile(elected_results_69$V2,probs=.95), 2))
##write(elected_upper_69, "elected_upper_69.tex")

elected_constant_69<-format(round(summary(elected_69_reg)$coefficients[1,1], 2))
##write(elected_constant_69, "elected_constant_69.tex")
elected_constant_lower_69<-format(round(quantile(elected_results_69$V1,probs=.05), 2))
##write(elected_constant_lower_69, "elected_constant_lower_69.tex")
elected_constant_upper_69<-format(round(quantile(elected_results_69$V1,probs=.95), 2))
##write(elected_constant_upper_69, "elected_constant_upper_69.tex")

# controls
pc_logit_c_69<-logitCoefficients(dips69_noin, c('victim', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(pc_logit_c_69))/2

electedc_results_69=as.data.frame(t(matrix(unlist(pc_logit_c_69), ncol=functional_iterations)))

electedc_69_glm<-glm(victim~factor(elected)+surname+dynastic+incumbent_orpastelected, data=dips69_noin, family="binomial")
electedc_elected_69<-format(round(summary(electedc_69_glm)$coefficients[2,1], 2))
##write(electedc_elected_69, "electedc_elected_69.tex")
electedc_69_AIC<-format(round(summary(electedc_69_glm)$aic, 2))
##write(electedc_69_AIC, "electedc_69_AIC.tex")
electedc_lower_69<-format(round(quantile(electedc_results_69$V2,probs=.05), 2))
##write(electedc_lower_69, "electedc_lower_69.tex")
electedc_upper_69<-format(round(quantile(electedc_results_69$V2,probs=.95), 2))
##write(electedc_upper_69, "electedc_upper_69.tex")

electedc_constant_69<-format(round(summary(electedc_69_glm)$coefficients[1,1], 2))
##write(electedc_constant_69, "electedc_constant_69.tex")
electedc_constant_lower_69<-format(round(quantile(electedc_results_69$V1,probs=.05), 2))
##write(electedc_constant_lower_69, "electedc_constant_lower_69.tex")
electedc_constant_upper_69<-format(round(quantile(electedc_results_69$V1,probs=.95), 2))
##write(electedc_constant_upper_69, "electedc_constant_upper_69.tex")


##party + district+ covariates
pc_logit_pdc_69<-logitCoefficients(dips69_noin, c('victim', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected', 'Partido', 'agrupacion_factor'), iterations)
functional_iterations=length(unlist(pc_logit_pdc_69))/2

electedpdc_results_69=as.data.frame(t(matrix(unlist(pc_logit_pdc_69), ncol=functional_iterations)))

electedpdc_69_glm<-glm(victim~factor(elected)+Partido+factor(agrupacion)+surname+dynastic+incumbent_orpastelected, data=dips69_noin, family="binomial")
electedpdc_elected_69<-format(round(summary(electedpdc_69_glm)$coefficients[2,1], 2))
electedpdc_69_AIC<-format(round(summary(electedpdc_69_glm)$aic, 2))
##write(electedpdc_69_AIC, "electedpdc_69_AIC.tex")
##write(electedpdc_elected_69, "electedpdc_elected_69.tex")
electedpdc_lower_69<-format(round(quantile(electedpdc_results_69$V2,probs=.05), 2))
##write(electedpdc_lower_69, "electedpdc_lower_69.tex")
electedpdc_upper_69<-format(round(quantile(electedpdc_results_69$V2,probs=.95), 2))
##write(electedpdc_upper_69, "electedpdc_upper_69.tex")

electedpdc_constant_69<-format(round(summary(electedpdc_69_glm)$coefficients[1,1], 2))
##write(electedpdc_constant_69, "electedpdc_constant_69.tex")
electedpdc_constant_lower_69<-format(round(quantile(electedpdc_results_69$V1,probs=.05), 2))
##write(electedpdc_constant_lower_69, "electedpdc_constant_lower_69.tex")
electedpdc_constant_upper_69<-format(round(quantile(electedpdc_results_69$V1,probs=.95), 2))
##write(electedpdc_constant_upper_69, "electedpdc_constant_upper_69.tex")


##excluding unelected
ar_logit_noc_69_nounelected<-logitCoefficients(dips69_nounelected, c('anyrepression', 'vote_perc'), iterations)
functional_iterations=length(unlist(ar_logit_noc_69_nounelected))/2

elected_anyrepression_results_69_nounelected=as.data.frame(t(matrix(unlist(ar_logit_noc_69_nounelected), ncol=functional_iterations)))
elected_anyrepression_69_nounelected_reg<-glm(anyrepression~vote_perc, data=dips69_noin, family="binomial")
elected_elected_anyrepression_69_nounelected<-format(round(summary(elected_anyrepression_69_nounelected_reg)$coefficients[2,1], 2))
##write(elected_elected_anyrepression_69_nounelected, "elected_elected_anyrepression_69_nounelected.tex")
elected_anyrepression_69_nounelected_AIC<-format(round(summary(elected_anyrepression_69_nounelected_reg)$aic, 2))
##write(elected_anyrepression_69_nounelected_AIC, "elected_anyrepression_69_nounelected_AIC.tex")

elected_anyrepression_lower_69_nounelected<-format(round(quantile(elected_anyrepression_results_69_nounelected$V2,probs=.05), 2))
##write(elected_anyrepression_lower_69_nounelected, "elected_anyrepression_lower_69_nounelected.tex")
elected_anyrepression_upper_69_nounelected<-format(round(quantile(elected_anyrepression_results_69_nounelected$V2,probs=.95), 2))
##write(elected_anyrepression_upper_69_nounelected, "elected_anyrepression_upper_69_nounelected.tex")

elected_constant_anyrepression_69_nounelected<-format(round(summary(elected_anyrepression_69_nounelected_reg)$coefficients[1,1], 2))
##write(elected_constant_anyrepression_69_nounelected, "elected_constant_anyrepression_69_nounelected.tex")
elected_constant_anyrepression_lower_69_nounelected<-format(round(quantile(elected_anyrepression_results_69_nounelected$V1,probs=.05), 2))
##write(elected_constant_anyrepression_lower_69_nounelected, "elected_constant_anyrepression_lower_69_nounelected.tex")
elected_constant_anyrepression_upper_69_nounelected<-format(round(quantile(elected_anyrepression_results_69_nounelected$V1,probs=.95), 2))
##write(elected_constant_anyrepression_upper_69_nounelected, "elected_constant_anyrepression_upper_69_nounelected.tex")

## controls
ar_logit_c_69_nounelected<-logitCoefficients(dips69_nounelected, c('anyrepression', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(ar_logit_c_69_nounelected))/2

electedc_anyrepression_results_69_nounelected=as.data.frame(t(matrix(unlist(ar_logit_c_69_nounelected), ncol=functional_iterations)))

electedc_anyrepression_69_nounelected_reg<-glm(anyrepression~vote_perc+surname+dynastic+incumbent_orpastelected, data=dips69_nounelected, family="binomial")
electedc_elected_anyrepression_69_nounelected<-format(round(summary(electedc_anyrepression_69_nounelected_reg)$coefficients[2,1], 2))
##write(electedc_elected_anyrepression_69_nounelected, "electedc_elected_anyrepression_69_nounelected.tex")
electedc_anyrepression_69_nounelected_AIC<-format(round(summary(electedc_anyrepression_69_nounelected_reg)$aic, 2))
##write(electedc_anyrepression_69_nounelected_AIC, "electedc_anyrepression_69_nounelected_AIC.tex")

electedc_anyrepression_lower_69_nounelected<-format(round(quantile(electedc_anyrepression_results_69_nounelected$V2,probs=.05), 2))
##write(electedc_anyrepression_lower_69_nounelected, "electedc_anyrepression_lower_69_nounelected.tex")
electedc_anyrepression_upper_69_nounelected<-format(round(quantile(electedc_anyrepression_results_69_nounelected$V2,probs=.95), 2))
##write(electedc_anyrepression_upper_69_nounelected, "electedc_anyrepression_upper_69_nounelected.tex")

electedc_constant_anyrepression_69_nounelected<-format(round(summary(electedc_anyrepression_69_nounelected_reg)$coefficients[1,1], 2))
##write(electedc_constant_anyrepression_69_nounelected, "electedc_constant_anyrepression_69_nounelected.tex")
electedc_constant_anyrepression_lower_69_nounelected<-format(round(quantile(electedc_anyrepression_results_69_nounelected$V1,probs=.05), 2))
##write(electedc_constant_anyrepression_lower_69_nounelected, "electedc_constant_anyrepression_lower_69_nounelected.tex")
electedc_constant_anyrepression_upper_69_nounelected<-format(round(quantile(electedc_anyrepression_results_69_nounelected$V1,probs=.95), 2))
##write(electedc_constant_anyrepression_upper_69_nounelected, "electedc_constant_anyrepression_upper_69_nounelected.tex")


###Party + district + covariates
dips69_nounelected$agrupacion_factor<-as.factor(dips69_nounelected$agrupacion)
ar_logit_pdc_69_nounelected<-logitCoefficients(dips69_nounelected, c('anyrepression', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected', 'Partido', 'agrupacion_factor'), iterations)
functional_iterations=length(unlist(ar_logit_pdc_69_nounelected))/2

electedpdc_anyrepression_results_69_nounelected=as.data.frame(t(matrix(unlist(ar_logit_pdc_69_nounelected), ncol=functional_iterations)))

electedpdc_anyrepression_69_nounelected_reg<-glm(anyrepression~vote_perc+Partido+factor(agrupacion)+surname+dynastic+incumbent_orpastelected, data=dips69_noin, family="binomial")
electedpdc_elected_anyrepression_69_nounelected<-format(round(summary(electedpdc_anyrepression_69_nounelected_reg)$coefficients[2,1], 2))
##write(electedpdc_elected_anyrepression_69_nounelected, "electedpdc_elected_anyrepression_69_nounelected.tex")
electedpdc_anyrepression_69_nounelected_AIC<-format(round(summary(electedpdc_anyrepression_69_nounelected_reg)$aic, 2))
##write(electedpdc_anyrepression_69_nounelected_AIC, "electedpdc_anyrepression_69_nounelected_AIC.tex")

electedpdc_anyrepression_lower_69_nounelected<-format(round(quantile(electedpdc_anyrepression_results_69_nounelected$V2,probs=.05), 2))
##write(electedpdc_anyrepression_lower_69_nounelected, "electedpdc_anyrepression_lower_69_nounelected.tex")
electedpdc_anyrepression_upper_69_nounelected<-format(round(quantile(electedpdc_anyrepression_results_69_nounelected$V2,probs=.95), 2))
##write(electedpdc_anyrepression_upper_69_nounelected, "electedpdc_anyrepression_upper_69_nounelected.tex")

electedpdc_constant_anyrepression_69_nounelected<-format(round(summary(electedpdc_anyrepression_69_nounelected_reg)$coefficients[1,1], 2))
##write(electedpdc_constant_anyrepression_69_nounelected, "electedpdc_constant_anyrepression_69_nounelected.tex")
electedpdc_constant_anyrepression_lower_69_nounelected<-format(round(quantile(electedpdc_anyrepression_results_69_nounelected$V1,probs=.05), 2))
##write(electedpdc_constant_anyrepression_lower_69_nounelected, "electedpdc_constant_anyrepression_lower_69_nounelected.tex")
electedpdc_constant_anyrepression_upper_69_nounelected<-format(round(quantile(electedpdc_anyrepression_results_69_nounelected$V1,probs=.95), 2))
##write(electedpdc_constant_anyrepression_upper_69_nounelected, "electedpdc_constant_anyrepression_upper_69_nounelected.tex")

########Physical coercion
pc_logit_noc_69_nounelected<-logitCoefficients(dips69_nounelected, c('victim', 'elected'), iterations)
functional_iterations=length(unlist(pc_logit_noc_69_nounelected))/2

#No controls
elected_results_69_nounelected=as.data.frame(t(matrix(unlist(pc_logit_noc_69_nounelected), ncol=functional_iterations)))

elected_69_nounelected_reg<-glm(victim~factor(elected), data=dips69_noin, family="binomial")
elected_elected_69_nounelected<-format(round(summary(elected_69_nounelected_reg)$coefficients[2,1], 2))
elected_69_nounelected_AIC<-format(round(summary(elected_69_nounelected_reg)$aic, 2))
elected_69_nounelected_obs<-dim(dips69_noin)[1]
##write(elected_elected_69_nounelected, "elected_elected_69_nounelected.tex")
##write(elected_69_nounelected_AIC, "elected_69_nounelected_AIC.tex")
##write(elected_69_nounelected_obs, "elected_69_nounelected_obs.tex")

elected_lower_69_nounelected<-format(round(quantile(elected_results_69_nounelected$V2,probs=.05), 2))
##write(elected_lower_69_nounelected, "elected_lower_69_nounelected.tex")
elected_upper_69_nounelected<-format(round(quantile(elected_results_69_nounelected$V2,probs=.95), 2))
##write(elected_upper_69_nounelected, "elected_upper_69_nounelected.tex")

elected_constant_69_nounelected<-format(round(summary(elected_69_nounelected_reg)$coefficients[1,1], 2))
##write(elected_constant_69_nounelected, "elected_constant_69_nounelected.tex")
elected_constant_lower_69_nounelected<-format(round(quantile(elected_results_69_nounelected$V1,probs=.05), 2))
##write(elected_constant_lower_69_nounelected, "elected_constant_lower_69_nounelected.tex")
elected_constant_upper_69_nounelected<-format(round(quantile(elected_results_69_nounelected$V1,probs=.95), 2))
##write(elected_constant_upper_69_nounelected, "elected_constant_upper_69_nounelected.tex")

# controls
pc_logit_c_69_nounelected<-logitCoefficients(dips69_nounelected, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)
functional_iterations=length(unlist(pc_logit_c_69_nounelected))/2

electedc_results_69_nounelected=as.data.frame(t(matrix(unlist(pc_logit_c_69_nounelected), ncol=functional_iterations)))

electedc_69_nounelected_glm<-glm(victim~factor(elected)+surname+dynastic+incumbent_orpastelected, data=dips69_noin, family="binomial")
electedc_elected_69_nounelected<-format(round(summary(electedc_69_nounelected_glm)$coefficients[2,1], 2))
##write(electedc_elected_69_nounelected, "electedc_elected_69_nounelected.tex")
electedc_69_nounelected_AIC<-format(round(summary(electedc_69_nounelected_glm)$aic, 2))
##write(electedc_69_nounelected_AIC, "electedc_69_nounelected_AIC.tex")
electedc_lower_69_nounelected<-format(round(quantile(electedc_results_69_nounelected$V2,probs=.05), 2))
##write(electedc_lower_69_nounelected, "electedc_lower_69_nounelected.tex")
electedc_upper_69_nounelected<-format(round(quantile(electedc_results_69_nounelected$V2,probs=.95), 2))
##write(electedc_upper_69_nounelected, "electedc_upper_69_nounelected.tex")

electedc_constant_69_nounelected<-format(round(summary(electedc_69_nounelected_glm)$coefficients[1,1], 2))
##write(electedc_constant_69_nounelected, "electedc_constant_69_nounelected.tex")
electedc_constant_lower_69_nounelected<-format(round(quantile(electedc_results_69_nounelected$V1,probs=.05), 2))
##write(electedc_constant_lower_69_nounelected, "electedc_constant_lower_69_nounelected.tex")
electedc_constant_upper_69_nounelected<-format(round(quantile(electedc_results_69_nounelected$V1,probs=.95), 2))
##write(electedc_constant_upper_69_nounelected, "electedc_constant_upper_69_nounelected.tex")



##party + district+ covariates
pc_logit_pdc_69_nounelected<-logitCoefficients(dips69_nounelected, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic', 'agrupacion_factor', 'Partido'), iterations)
functional_iterations=length(unlist(pc_logit_pdc_69_nounelected))/2

electedpdc_results_69_nounelected=as.data.frame(t(matrix(unlist(pc_logit_pdc_69_nounelected), ncol=functional_iterations)))

electedpdc_69_nounelected_glm<-glm(victim~factor(elected)+Partido+factor(agrupacion)+surname+dynastic+incumbent_orpastelected, data=dips69_noin, family="binomial")
electedpdc_elected_69_nounelected<-format(round(summary(electedpdc_69_nounelected_glm)$coefficients[2,1], 2))
electedpdc_69_nounelected_AIC<-format(round(summary(electedpdc_69_nounelected_glm)$aic, 2))
##write(electedpdc_69_nounelected_AIC, "electedpdc_69_nounelected_AIC.tex")
##write(electedpdc_elected_69_nounelected, "electedpdc_elected_69_nounelected.tex")
electedpdc_lower_69_nounelected<-format(round(quantile(electedpdc_results_69_nounelected$V2,probs=.05), 2))
##write(electedpdc_lower_69_nounelected, "electedpdc_lower_69_nounelected.tex")
electedpdc_upper_69_nounelected<-format(round(quantile(electedpdc_results_69_nounelected$V2,probs=.95), 2))
##write(electedpdc_upper_69_nounelected, "electedpdc_upper_69_nounelected.tex")

electedpdc_constant_69_nounelected<-format(round(summary(electedpdc_69_nounelected_glm)$coefficients[1,1], 2))
##write(electedpdc_constant_69_nounelected, "electedpdc_constant_69_nounelected.tex")
electedpdc_constant_lower_69_nounelected<-format(round(quantile(electedpdc_results_69_nounelected$V1,probs=.05), 2))
##write(electedpdc_constant_lower_69_nounelected, "electedpdc_constant_lower_69_nounelected.tex")
electedpdc_constant_upper_69_nounelected<-format(round(quantile(electedpdc_results_69_nounelected$V1,probs=.95), 2))
##write(electedpdc_constant_upper_69_nounelected, "electedpdc_constant_upper_69_nounelected.tex")





##left leaning, no unelected
ar_logit_noc_69_leftDC_nounelected<-logitCoefficients(dips69_leftDC_nounelected, c('anyrepression', 'vote_perc'), iterations)
functional_iterations=length(unlist(ar_logit_noc_69_leftDC_nounelected))/2

elected_anyrepression_results_69_leftDC_nounelected=as.data.frame(t(matrix(unlist(ar_logit_noc_69_leftDC_nounelected), ncol=functional_iterations)))
elected_anyrepression_69_leftDC_nounelected_reg<-glm(anyrepression~vote_perc, data=dips69_noin, family="binomial")
elected_elected_anyrepression_69_leftDC_nounelected<-format(round(summary(elected_anyrepression_69_leftDC_nounelected_reg)$coefficients[2,1], 2))
##write(elected_elected_anyrepression_69_leftDC_nounelected, "elected_elected_anyrepression_69_leftDC_nounelected.tex")
elected_anyrepression_69_leftDC_nounelected_AIC<-format(round(summary(elected_anyrepression_69_leftDC_nounelected_reg)$aic, 2))
##write(elected_anyrepression_69_leftDC_nounelected_AIC, "elected_anyrepression_69_leftDC_nounelected_AIC.tex")

elected_anyrepression_lower_69_leftDC_nounelected<-format(round(quantile(elected_anyrepression_results_69_leftDC_nounelected$V2,probs=.05), 2))
##write(elected_anyrepression_lower_69_leftDC_nounelected, "elected_anyrepression_lower_69_leftDC_nounelected.tex")
elected_anyrepression_upper_69_leftDC_nounelected<-format(round(quantile(elected_anyrepression_results_69_leftDC_nounelected$V2,probs=.95), 2))
##write(elected_anyrepression_upper_69_leftDC_nounelected, "elected_anyrepression_upper_69_leftDC_nounelected.tex")

elected_constant_anyrepression_69_leftDC_nounelected<-format(round(summary(elected_anyrepression_69_leftDC_nounelected_reg)$coefficients[1,1], 2))
##write(elected_constant_anyrepression_69_leftDC_nounelected, "elected_constant_anyrepression_69_leftDC_nounelected.tex")
elected_constant_anyrepression_lower_69_leftDC_nounelected<-format(round(quantile(elected_anyrepression_results_69_leftDC_nounelected$V1,probs=.05), 2))
##write(elected_constant_anyrepression_lower_69_leftDC_nounelected, "elected_constant_anyrepression_lower_69_leftDC_nounelected.tex")
elected_constant_anyrepression_upper_69_leftDC_nounelected<-format(round(quantile(elected_anyrepression_results_69_leftDC_nounelected$V1,probs=.95), 2))
##write(elected_constant_anyrepression_upper_69_leftDC_nounelected, "elected_constant_anyrepression_upper_69_leftDC_nounelected.tex")

## controls
ar_logit_c_69_leftDC_nounelected<-logitCoefficients(dips69_leftDC_nounelected, c('anyrepression', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(ar_logit_c_69_leftDC_nounelected))/2

electedc_anyrepression_results_69_leftDC_nounelected=as.data.frame(t(matrix(unlist(ar_logit_c_69_leftDC_nounelected), ncol=functional_iterations)))

electedc_anyrepression_69_leftDC_nounelected_reg<-glm(anyrepression~vote_perc+surname+dynastic+incumbent_orpastelected, data=dips69_leftDC_nounelected, family="binomial")
electedc_elected_anyrepression_69_leftDC_nounelected<-format(round(summary(electedc_anyrepression_69_leftDC_nounelected_reg)$coefficients[2,1], 2))
##write(electedc_elected_anyrepression_69_leftDC_nounelected, "electedc_elected_anyrepression_69_leftDC_nounelected.tex")
electedc_anyrepression_69_leftDC_nounelected_AIC<-format(round(summary(electedc_anyrepression_69_leftDC_nounelected_reg)$aic, 2))
##write(electedc_anyrepression_69_leftDC_nounelected_AIC, "electedc_anyrepression_69_leftDC_nounelected_AIC.tex")

electedc_anyrepression_lower_69_leftDC_nounelected<-format(round(quantile(electedc_anyrepression_results_69_leftDC_nounelected$V2,probs=.05), 2))
##write(electedc_anyrepression_lower_69_leftDC_nounelected, "electedc_anyrepression_lower_69_leftDC_nounelected.tex")
electedc_anyrepression_upper_69_leftDC_nounelected<-format(round(quantile(electedc_anyrepression_results_69_leftDC_nounelected$V2,probs=.95), 2))
##write(electedc_anyrepression_upper_69_leftDC_nounelected, "electedc_anyrepression_upper_69_leftDC_nounelected.tex")

electedc_constant_anyrepression_69_leftDC_nounelected<-format(round(summary(electedc_anyrepression_69_leftDC_nounelected_reg)$coefficients[1,1], 2))
##write(electedc_constant_anyrepression_69_leftDC_nounelected, "electedc_constant_anyrepression_69_leftDC_nounelected.tex")
electedc_constant_anyrepression_lower_69_leftDC_nounelected<-format(round(quantile(electedc_anyrepression_results_69_leftDC_nounelected$V1,probs=.05), 2))
##write(electedc_constant_anyrepression_lower_69_leftDC_nounelected, "electedc_constant_anyrepression_lower_69_leftDC_nounelected.tex")
electedc_constant_anyrepression_upper_69_leftDC_nounelected<-format(round(quantile(electedc_anyrepression_results_69_leftDC_nounelected$V1,probs=.95), 2))
##write(electedc_constant_anyrepression_upper_69_leftDC_nounelected, "electedc_constant_anyrepression_upper_69_leftDC_nounelected.tex")


###Party + district + covariates
dips69_leftDC_nounelected$agrupacion_factor<-as.factor(dips69_leftDC_nounelected$agrupacion)
ar_logit_pdc_69_leftDC_nounelected<-logitCoefficients(dips69_leftDC_nounelected, c('anyrepression', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'Partido'), iterations)
functional_iterations=length(unlist(ar_logit_pdc_69_leftDC_nounelected))/2

electedpdc_anyrepression_results_69_leftDC_nounelected=as.data.frame(t(matrix(unlist(ar_logit_pdc_69_leftDC_nounelected), ncol=functional_iterations)))

electedpdc_anyrepression_69_leftDC_nounelected_reg<-glm(anyrepression~vote_perc+Partido+factor(agrupacion)+surname+dynastic+incumbent_orpastelected, data=dips69_noin, family="binomial")
electedpdc_elected_anyrepression_69_leftDC_nounelected<-format(round(summary(electedpdc_anyrepression_69_leftDC_nounelected_reg)$coefficients[2,1], 2))
##write(electedpdc_elected_anyrepression_69_leftDC_nounelected, "electedpdc_elected_anyrepression_69_leftDC_nounelected.tex")
electedpdc_anyrepression_69_leftDC_nounelected_AIC<-format(round(summary(electedpdc_anyrepression_69_leftDC_nounelected_reg)$aic, 2))
##write(electedpdc_anyrepression_69_leftDC_nounelected_AIC, "electedpdc_anyrepression_69_leftDC_nounelected_AIC.tex")

electedpdc_anyrepression_lower_69_leftDC_nounelected<-format(round(quantile(electedpdc_anyrepression_results_69_leftDC_nounelected$V2,probs=.05), 2))
##write(electedpdc_anyrepression_lower_69_leftDC_nounelected, "electedpdc_anyrepression_lower_69_leftDC_nounelected.tex")
electedpdc_anyrepression_upper_69_leftDC_nounelected<-format(round(quantile(electedpdc_anyrepression_results_69_leftDC_nounelected$V2,probs=.95), 2))
##write(electedpdc_anyrepression_upper_69_leftDC_nounelected, "electedpdc_anyrepression_upper_69_leftDC_nounelected.tex")

electedpdc_constant_anyrepression_69_leftDC_nounelected<-format(round(summary(electedpdc_anyrepression_69_leftDC_nounelected_reg)$coefficients[1,1], 2))
##write(electedpdc_constant_anyrepression_69_leftDC_nounelected, "electedpdc_constant_anyrepression_69_leftDC_nounelected.tex")
electedpdc_constant_anyrepression_lower_69_leftDC_nounelected<-format(round(quantile(electedpdc_anyrepression_results_69_leftDC_nounelected$V1,probs=.05), 2))
##write(electedpdc_constant_anyrepression_lower_69_leftDC_nounelected, "electedpdc_constant_anyrepression_lower_69_leftDC_nounelected.tex")
electedpdc_constant_anyrepression_upper_69_leftDC_nounelected<-format(round(quantile(electedpdc_anyrepression_results_69_leftDC_nounelected$V1,probs=.95), 2))
##write(electedpdc_constant_anyrepression_upper_69_leftDC_nounelected, "electedpdc_constant_anyrepression_upper_69_leftDC_nounelected.tex")

########Physical coercion

#No controls
pc_logit_noc_69_leftDC_nounelected<-logitCoefficients(dips69_leftDC_nounelected, c('victim', 'elected'), iterations)
functional_iterations=length(unlist(pc_logit_noc_69_leftDC_nounelected))/2

elected_results_69_leftDC_nounelected=as.data.frame(t(matrix(unlist(pc_logit_noc_69_leftDC_nounelected), ncol=functional_iterations)))

elected_69_leftDC_nounelected_reg<-glm(victim~factor(elected), data=dips69_noin, family="binomial")
elected_elected_69_leftDC_nounelected<-format(round(summary(elected_69_leftDC_nounelected_reg)$coefficients[2,1], 2))
elected_69_leftDC_nounelected_AIC<-format(round(summary(elected_69_leftDC_nounelected_reg)$aic, 2))
elected_69_leftDC_nounelected_obs<-dim(dips69_noin)[1]
##write(elected_elected_69_leftDC_nounelected, "elected_elected_69_leftDC_nounelected.tex")
##write(elected_69_leftDC_nounelected_AIC, "elected_69_leftDC_nounelected_AIC.tex")
##write(elected_69_leftDC_nounelected_obs, "elected_69_leftDC_nounelected_obs.tex")

elected_lower_69_leftDC_nounelected<-format(round(quantile(elected_results_69_leftDC_nounelected$V2,probs=.05), 2))
##write(elected_lower_69_leftDC_nounelected, "elected_lower_69_leftDC_nounelected.tex")
elected_upper_69_leftDC_nounelected<-format(round(quantile(elected_results_69_leftDC_nounelected$V2,probs=.95), 2))
##write(elected_upper_69_leftDC_nounelected, "elected_upper_69_leftDC_nounelected.tex")

elected_constant_69_leftDC_nounelected<-format(round(summary(elected_69_leftDC_nounelected_reg)$coefficients[1,1], 2))
##write(elected_constant_69_leftDC_nounelected, "elected_constant_69_leftDC_nounelected.tex")
elected_constant_lower_69_leftDC_nounelected<-format(round(quantile(elected_results_69_leftDC_nounelected$V1,probs=.05), 2))
##write(elected_constant_lower_69_leftDC_nounelected, "elected_constant_lower_69_leftDC_nounelected.tex")
elected_constant_upper_69_leftDC_nounelected<-format(round(quantile(elected_results_69_leftDC_nounelected$V1,probs=.95), 2))
##write(elected_constant_upper_69_leftDC_nounelected, "elected_constant_upper_69_leftDC_nounelected.tex")

# controls
pc_logit_c_69_leftDC_nounelected<-logitCoefficients(dips69_leftDC_nounelected, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic'), iterations)
functional_iterations=length(unlist(pc_logit_c_69_leftDC_nounelected))/2

electedc_results_69_leftDC_nounelected=as.data.frame(t(matrix(unlist(pc_logit_c_69_leftDC_nounelected), ncol=functional_iterations)))

electedc_69_leftDC_nounelected_glm<-glm(victim~factor(elected)+surname+dynastic+incumbent_orpastelected, data=dips69_noin, family="binomial")
electedc_elected_69_leftDC_nounelected<-format(round(summary(electedc_69_leftDC_nounelected_glm)$coefficients[2,1], 2))
##write(electedc_elected_69_leftDC_nounelected, "electedc_elected_69_leftDC_nounelected.tex")
electedc_69_leftDC_nounelected_AIC<-format(round(summary(electedc_69_leftDC_nounelected_glm)$aic, 2))
##write(electedc_69_leftDC_nounelected_AIC, "electedc_69_leftDC_nounelected_AIC.tex")
electedc_lower_69_leftDC_nounelected<-format(round(quantile(electedc_results_69_leftDC_nounelected$V2,probs=.05), 2))
##write(electedc_lower_69_leftDC_nounelected, "electedc_lower_69_leftDC_nounelected.tex")
electedc_upper_69_leftDC_nounelected<-format(round(quantile(electedc_results_69_leftDC_nounelected$V2,probs=.95), 2))
##write(electedc_upper_69_leftDC_nounelected, "electedc_upper_69_leftDC_nounelected.tex")

electedc_constant_69_leftDC_nounelected<-format(round(summary(electedc_69_leftDC_nounelected_glm)$coefficients[1,1], 2))
##write(electedc_constant_69_leftDC_nounelected, "electedc_constant_69_leftDC_nounelected.tex")
electedc_constant_lower_69_leftDC_nounelected<-format(round(quantile(electedc_results_69_leftDC_nounelected$V1,probs=.05), 2))
##write(electedc_constant_lower_69_leftDC_nounelected, "electedc_constant_lower_69_leftDC_nounelected.tex")
electedc_constant_upper_69_leftDC_nounelected<-format(round(quantile(electedc_results_69_leftDC_nounelected$V1,probs=.95), 2))
##write(electedc_constant_upper_69_leftDC_nounelected, "electedc_constant_upper_69_leftDC_nounelected.tex")



##party + district+ covariates
pc_logit_pdc_69_leftDC_nounelected<-logitCoefficients(dips69_leftDC_nounelected, c('victim', 'elected', 'surname', 'incumbent_orpastelected', 'dynastic', 'agrupacion_factor', 'Partido'), iterations)
functional_iterations=length(unlist(pc_logit_pdc_69_leftDC_nounelected))/2

electedpdc_results_69_leftDC_nounelected=as.data.frame(t(matrix(unlist(pc_logit_pdc_69_leftDC_nounelected), ncol=functional_iterations)))

electedpdc_69_leftDC_nounelected_glm<-glm(victim~factor(elected)+Partido+factor(agrupacion)+surname+dynastic+incumbent_orpastelected, data=dips69_noin, family="binomial")
electedpdc_elected_69_leftDC_nounelected<-format(round(summary(electedpdc_69_leftDC_nounelected_glm)$coefficients[2,1], 2))
electedpdc_69_leftDC_nounelected_AIC<-format(round(summary(electedpdc_69_leftDC_nounelected_glm)$aic, 2))
##write(electedpdc_69_leftDC_nounelected_AIC, "electedpdc_69_leftDC_nounelected_AIC.tex")
##write(electedpdc_elected_69_leftDC_nounelected, "electedpdc_elected_69_leftDC_nounelected.tex")
electedpdc_lower_69_leftDC_nounelected<-format(round(quantile(electedpdc_results_69_leftDC_nounelected$V2,probs=.05), 2))
##write(electedpdc_lower_69_leftDC_nounelected, "electedpdc_lower_69_leftDC_nounelected.tex")
electedpdc_upper_69_leftDC_nounelected<-format(round(quantile(electedpdc_results_69_leftDC_nounelected$V2,probs=.95), 2))
##write(electedpdc_upper_69_leftDC_nounelected, "electedpdc_upper_69_leftDC_nounelected.tex")

electedpdc_constant_69_leftDC_nounelected<-format(round(summary(electedpdc_69_leftDC_nounelected_glm)$coefficients[1,1], 2))
##write(electedpdc_constant_69_leftDC_nounelected, "electedpdc_constant_69_leftDC_nounelected.tex")
electedpdc_constant_lower_69_leftDC_nounelected<-format(round(quantile(electedpdc_results_69_leftDC_nounelected$V1,probs=.05), 2))
##write(electedpdc_constant_lower_69_leftDC_nounelected, "electedpdc_constant_lower_69_leftDC_nounelected.tex")
electedpdc_constant_upper_69_leftDC_nounelected<-format(round(quantile(electedpdc_results_69_leftDC_nounelected$V1,probs=.95), 2))
##write(electedpdc_constant_upper_69_leftDC_nounelected, "electedpdc_constant_upper_69_leftDC_nounelected.tex")



####################################################################
##########Figure C4: Close elections diff-in-means, 1969 ###########
####################################################################
dips69_noin$levels<-ifelse(dips69_noin$quotient_diff<quantile(dips69_noin$quotient_diff[dips69_noin$elected==0], .5), 0, 1)
dips69_noin$levels<-ifelse(dips69_noin$quotient_diff>quantile(dips69_noin$quotient_diff[dips69_noin$elected==1], .5), 3, dips69_noin$levels)
dips69_noin$levels<-ifelse(dips69_noin$elected==1&dips69_noin$levels==1, 2, dips69_noin$levels)

dips69_leftDC_nounelected$levels<-ifelse(dips69_leftDC_nounelected$quotient_diff<quantile(dips69_leftDC_nounelected$quotient_diff[dips69_leftDC_nounelected$elected==0], .5), 0, 1)
dips69_leftDC_nounelected$levels<-ifelse(dips69_leftDC_nounelected$quotient_diff>quantile(dips69_leftDC_nounelected$quotient_diff[dips69_leftDC_nounelected$elected==1], .5), 3, dips69_leftDC_nounelected$levels)
dips69_leftDC_nounelected$levels<-ifelse(dips69_leftDC_nounelected$elected==1&dips69_leftDC_nounelected$levels==1, 2, dips69_leftDC_nounelected$levels)

anyrepression_plot69<-whiskerPlotLevels(dips69_noin, dips69_noin$anyrepression, " ", "Overall repression mean", "All candidates")
anyrepression_leftDC_plot69<-whiskerPlotLevels(dips69_leftDC_nounelected, dips69_leftDC_nounelected$anyrepression, " ", " ", "Left-Leaning Candidates")

physicalcoercion_plot69<-whiskerPlotLevels(dips69_noin, dips69_noin$victim, "Group", "Physical coercion mean", " ")
physicalcoercion_leftDC_plot69<-whiskerPlotLevels(dips69_leftDC_nounelected, dips69_leftDC_nounelected$victim, "Group", " ", " ")

#pdf("victim_anyrepressionplots69.pdf", width=10, height=8, family="Times New Roman")
multiplot(anyrepression_plot69, physicalcoercion_plot69, anyrepression_leftDC_plot69, physicalcoercion_leftDC_plot69, cols=2)
#dev.off()


###########################################################
#################Table C7: LLR 1969########################
###########################################################

rdd_all_noin69<-RDestimate(victim~quotient_diff, data=dips69_noin, cluster=dips69_noin$Distrito)
rdd_all_party_noin69<-RDestimate(victim~quotient_diff|Partido, data=dips69_noin, cluster=dips69_noin$Distrito)

#write(format(round(rdd_all_noin69$est[1], 2)), "late_noin69.tex")
#write(format(round(rdd_all_noin69$est[2], 2)), "half_bw_noin69.tex")
#write(format(round(rdd_all_noin69$est[3], 2)), "double_bw_noin69.tex")

#write(format(round(rdd_all_party_noin69$est[1], 2)), "latep_noin69.tex")
#write(format(round(rdd_all_party_noin69$est[2], 2)), "halfp_noin69.tex")
#write(format(round(rdd_all_party_noin69$est[3], 2)), "doublep_noin69.tex")

#write(rdd_all_noin69$obs[1], "obs_late_noin69.tex")
#write(rdd_all_noin69$obs[2], "obs_half_late_noin69.tex")
#write(rdd_all_noin69$obs[3], "obs_double_late_noin69.tex")

#write(format(round(rdd_all_noin69$bw[1],2)), "bw_late_noin69.tex")
#write(format(round(rdd_all_noin69$bw[2],2)), "bw_half_late_noin69.tex")
#write(format(round(rdd_all_noin69$bw[3],2)), "bw_double_late_noin69.tex")

dips69_noin$distritos_num<-as.numeric(dips69_noin$Distrito)

X <-dips69_noin$quotient_diff
Xl <- (X < 0) * X
Xr <- (X >= 0) * X
Tr <- as.integer(X >= 0)
Y <- dips69_noin$victim
cluster<-dips69_noin$distritos_num
bw=IKbandwidth(X = X, Y = Y, cutpoint = 0, kernel = "triangular")
w<-kernelwts(dips69_noin$quotient_diff, 0, bw=bw, kernel="triangular")
whalf<-kernelwts(dips69_noin$quotient_diff, 0, bw=bw/2, kernel="triangular")
w2<-kernelwts(dips69_noin$quotient_diff, 0, bw=2*bw, kernel="triangular")
partydummies<-dummy(dips69_noin$Partido)
head(partydummies)
colnames(partydummies)<-c("DN", "PC", "PDC", "PN", "PR", "PS", 'PSD', 'USP')
partydummies<-data.frame(partydummies)

PC<-partydummies$PC
PDC<-partydummies$PDC
PN<-partydummies$PN
PR<-partydummies$PR
PS<-partydummies$PS
PSD<-partydummies$PSD
USP<-partydummies$USP

cluster <- dips69_noin$distritos_num
data<-data.frame(Y, Tr, X, Xl, Xr, w, whalf, w2, cluster, PC, PDC, PN, PR, PS, PSD, USP)

distritos_num<-unique(cluster)
n=length(distritos_num)
LLR_noin69=list()
LLR_half_noin69=list()
LLR_2_noin69=list()
LLR_party_noin69=list()
LLR_party_half_noin69=list()
LLR_party_2_noin69=list()
for (j in 1:iterations){
  v=sample(distritos_num, n, replace=TRUE)
  newdata=NULL
  
  for (i in 1:n){
    newdata=rbind(newdata, subset(data, data$cluster==v[i]))
  }
  mod691<-lm(Y ~ Tr + Xl + Xr, weights = w, data = subset(newdata, w > 0))
  mod692<-lm(Y ~ Tr + Xl + Xr, weights=whalf, data=subset(newdata, whalf>0))
  mod693<-lm(Y ~ Tr + Xl + Xr, weights=w2, data=subset(newdata, w2>0))
  
  mod694<-lm(Y ~ Tr + Xl + Xr+PC+PDC+PN+PR+PS+PSD+USP, weights = w, data = subset(newdata, w > 0))
  mod695<-lm(Y ~ Tr + Xl + Xr+PC+PDC+PN+PR+PS+PSD+USP, weights = whalf, data = subset(newdata, whalf > 0))
  mod696<-lm(Y ~ Tr + Xl + Xr+PC+PDC+PN+PR+PS+PSD+USP, weights = w2, data = subset(newdata, w2 > 0))
  
  LLR_noin69[[j]]=coefficients(mod691)
  LLR_half_noin69[[j]]=coefficients(mod692)
  LLR_2_noin69[[j]]=coefficients(mod693)
  LLR_party_noin69[[j]]=coefficients(mod694)
  LLR_party_half_noin69[[j]]=coefficients(mod695)
  LLR_party_2_noin69[[j]]=coefficients(mod696)
}
LLR_results_noin69=as.data.frame(t(matrix(unlist(LLR_noin69), ncol=iterations)))
names(LLR_results_noin69)=names(mod691$coefficients)
LLR_half_results_noin69=as.data.frame(t(matrix(unlist(LLR_half_noin69), ncol=iterations)))
names(LLR_half_results_noin69)=names(mod692$coefficients)
LLR_2_results_noin69=as.data.frame(t(matrix(unlist(LLR_2_noin69), ncol=iterations)))
names(LLR_2_results_noin69)=names(mod693$coefficients)
LLR_party_results_noin69=as.data.frame(t(matrix(unlist(LLR_party_noin69), ncol=iterations)))
names(LLR_party_results_noin69)=names(mod694$coefficients)
LLR_party_half_results_noin69=as.data.frame(t(matrix(unlist(LLR_party_half_noin69), ncol=iterations)))
names(LLR_party_half_results_noin69)=names(mod694$coefficients)
LLR_party_2_results_noin69=as.data.frame(t(matrix(unlist(LLR_party_2_noin69), ncol=iterations)))
names(LLR_party_2_results_noin69)=names(mod695$coefficients)

late_lower_noin69<-format(round(quantile(LLR_results_noin69$Tr, probs=.05), 2))
late_upper_noin69<-format(round(quantile(LLR_results_noin69$Tr, probs=.95), 2))
#write(late_lower_noin69, "late_lower_noin69.tex")
#write(late_upper_noin69, "late_upper_noin69.tex")

late_lower_noin69_95<-format(round(quantile(LLR_results_noin69$Tr, probs=.025), 2))
late_upper_noin69_95<-format(round(quantile(LLR_results_noin69$Tr, probs=.975), 2))
#write(late_lower_noin69_95, "late_lower_noin69_95.tex")
#write(late_upper_noin69_95, "late_upper_noin69_95.tex")

late_half_lower_noin69<-format(round(quantile(LLR_half_results_noin69$Tr, probs=.05), 2))
late_half_upper_noin69<-format(round(quantile(LLR_half_results_noin69$Tr, probs=.95), 2))
#write(late_half_lower_noin69, "late_half_lower_noin69.tex")
#write(late_half_upper_noin69, "late_half_upper_noin69.tex")

late_half_lower_noin69_95<-format(round(quantile(LLR_half_results_noin69$Tr, probs=.025), 2))
late_half_upper_noin69_95<-format(round(quantile(LLR_half_results_noin69$Tr, probs=.975), 2))
#write(late_half_lower_noin69_95, "late_half_lower_noin69_95.tex")
#write(late_half_upper_noin69_95, "late_half_upper_noin69_95.tex")

late_2_lower_noin69<-format(round(quantile(LLR_2_results_noin69$Tr, probs=.05), 2))
late_2_upper_noin69<-format(round(quantile(LLR_2_results_noin69$Tr, probs=.95), 2))
#write(late_2_lower_noin69, "late_2_lower_noin69.tex")
#write(late_2_upper_noin69, "late_2_upper_noin69.tex")

late_2_lower_noin69_95<-format(round(quantile(LLR_2_results_noin69$Tr, probs=.025), 2))
late_2_upper_noin69_95<-format(round(quantile(LLR_2_results_noin69$Tr, probs=.975), 2))
#write(late_2_lower_noin69_95, "late_2_lower_noin69_95.tex")
#write(late_2_upper_noin69_95, "late_2_upper_noin69_95.tex")

late_party_lower_noin69<-format(round(quantile(LLR_party_results_noin69$Tr, probs=.05), 2))
late_party_upper_noin69<-format(round(quantile(LLR_party_results_noin69$Tr, probs=.95), 2))
#write(late_party_lower_noin69, "late_party_lower_noin69.tex")
#write(late_party_upper_noin69, "late_party_upper_noin69.tex")

late_party_lower_noin69_95<-format(round(quantile(LLR_party_results_noin69$Tr, probs=.025), 2))
late_party_upper_noin69_95<-format(round(quantile(LLR_party_results_noin69$Tr, probs=.975), 2))
#write(late_party_lower_noin69_95, "late_party_lower_noin69_95.tex")
#write(late_party_upper_noin69_95, "late_party_upper_noin69_95.tex")

late_party_half_lower_noin69<-format(round(quantile(LLR_party_half_results_noin69$Tr, probs=.05), 2))
late_party_half_upper_noin69<-format(round(quantile(LLR_party_half_results_noin69$Tr, probs=.95), 2))
#write(late_party_half_lower_noin69, "late_party_half_lower_noin69.tex")
#write(late_party_half_upper_noin69, "late_party_half_upper_noin69.tex")

late_party_half_lower_noin69_95<-format(round(quantile(LLR_party_half_results_noin69$Tr, probs=.025), 2))
late_party_half_upper_noin69_95<-format(round(quantile(LLR_party_half_results_noin69$Tr, probs=.975), 2))
#write(late_party_half_lower_noin69_95, "late_party_half_lower_noin69_95.tex")
#write(late_party_half_upper_noin69_95, "late_party_half_upper_noin69_95.tex")

late_party_2_lower_noin69<-format(round(quantile(LLR_party_2_results_noin69$Tr, probs=.05), 2))
late_party_2_upper_noin69<-format(round(quantile(LLR_party_2_results_noin69$Tr, probs=.95), 2))
#write(late_party_2_lower_noin69, "late_party_2_lower_noin69.tex")
#write(late_party_2_upper_noin69, "late_party_2_upper_noin69.tex")

late_party_2_lower_noin69_95<-format(round(quantile(LLR_party_2_results_noin69$Tr, probs=.025), 2))
late_party_2_upper_noin69_95<-format(round(quantile(LLR_party_2_results_noin69$Tr, probs=.975), 2))
#write(late_party_2_lower_noin69_95, "late_party_2_lower_noin69_95.tex")
#write(late_party_2_upper_noin69_95, "late_party_2_upper_noin69_95.tex")


##anyrepression
rdd_all_anyrepression_noin69<-RDestimate(anyrepression~quotient_diff, data=dips69_noin, cluster=dips69_noin$Distrito)
summary(rdd_all_anyrepression_noin69)
rdd_all_party_anyrepression_noin69<-RDestimate(anyrepression~quotient_diff|Partido, data=dips69_noin, cluster=dips69_noin$Distrito, bw=2)
summary(rdd_all_party_anyrepression_noin69)

#write(format(round(rdd_all_anyrepression_noin69$est[1], 2)), "late_anyrepression_noin69.tex")
#write(format(round(rdd_all_anyrepression_noin69$est[2], 2)), "half_bw_anyrepression_noin69.tex")
#write(format(round(rdd_all_anyrepression_noin69$est[3], 2)), "double_bw_anyrepression_noin69.tex")

#write(format(round(rdd_all_party_anyrepression_noin69$est[1], 2)), "latep_anyrepression_noin69.tex")
#write(format(round(rdd_all_party_anyrepression_noin69$est[2], 2)), "halfp_anyrepression_noin69.tex")
#write(format(round(rdd_all_party_anyrepression_noin69$est[3], 2)), "doublep_anyrepression_noin69.tex")

#write(rdd_all_anyrepression_noin69$obs[1], "obs_late_anyrepression_noin69.tex")
#write(rdd_all_anyrepression_noin69$obs[2], "obs_half_late_anyrepression_noin69.tex")
#write(rdd_all_anyrepression_noin69$obs[3], "obs_double_late_anyrepression_noin69.tex")

#write(format(round(rdd_all_anyrepression_noin69$bw[1],2)), "bw_late_anyrepression_noin69.tex")
#write(format(round(rdd_all_anyrepression_noin69$bw[2],2)), "bw_half_late_anyrepression_noin69.tex")
#write(format(round(rdd_all_anyrepression_noin69$bw[3],2)), "bw_double_late_anyrepression_noin69.tex")

dips69_noin$distritos_num<-as.numeric(dips69_noin$Distrito)

X <-dips69_noin$quotient_diff
Xl <- (X < 0) * X
Xr <- (X >= 0) * X
Tr <- as.integer(X >= 0)
Y <- dips69_noin$anyrepression
cluster<-dips69_noin$distritos_num
bw=IKbandwidth(X = X, Y = Y, cutpoint = 0, kernel = "triangular")
w<-kernelwts(dips69_noin$quotient_diff, 0, bw=bw, kernel="triangular")
whalf<-kernelwts(dips69_noin$quotient_diff, 0, bw=bw/2, kernel="triangular")
w2<-kernelwts(dips69_noin$quotient_diff, 0, bw=2*bw, kernel="triangular")

cluster <- dips69_noin$distritos_num
data<-data.frame(Y, Tr, X, Xl, Xr, w, whalf, w2, cluster, PC, PDC, PN, PR, PS, PSD, USP)

distritos_num<-unique(cluster)
n=length(distritos_num)
LLR_anyrepression_noin69=list()
LLR_half_anyrepression_noin69=list()
LLR_2_anyrepression_noin69=list()
LLR_party_anyrepression_noin69=list()
LLR_party_half_anyrepression_noin69=list()
LLR_party_2_anyrepression_noin69=list()
for (j in 1:iterations){
  v=sample(distritos_num, n, replace=TRUE)
  newdata=NULL
  
  for (i in 1:n){
    newdata=rbind(newdata, subset(data, data$cluster==v[i]))
  }
  mod69left1<-lm(Y ~ Tr + Xl + Xr, weights = w, data = subset(newdata, w > 0))
  mod69left2<-lm(Y ~ Tr + Xl + Xr, weights=whalf, data=subset(newdata, whalf>0))
  mod69left3<-lm(Y ~ Tr + Xl + Xr, weights=w2, data=subset(newdata, w2>0))
  
  mod69left4<-lm(Y ~ Tr + Xl + Xr+PC+PDC+PN+PR+PS+PSD+USP, weights = w, data = subset(newdata, w > 0))
  mod69left5<-lm(Y ~ Tr + Xl + Xr+PC+PDC+PN+PR+PS+PSD+USP, weights = whalf, data = subset(newdata, whalf > 0))
  mod69left6<-lm(Y ~ Tr + Xl + Xr+PC+PDC+PN+PR+PS+PSD+USP, weights = w2, data = subset(newdata, w2 > 0))
  
  LLR_anyrepression_noin69[[j]]=coefficients(mod69left1)
  LLR_half_anyrepression_noin69[[j]]=coefficients(mod69left2)
  LLR_2_anyrepression_noin69[[j]]=coefficients(mod69left3)
  LLR_party_anyrepression_noin69[[j]]=coefficients(mod69left4)
  LLR_party_half_anyrepression_noin69[[j]]=coefficients(mod69left5)
  LLR_party_2_anyrepression_noin69[[j]]=coefficients(mod69left6)
}
LLR_results_anyrepression_noin69=as.data.frame(t(matrix(unlist(LLR_anyrepression_noin69), ncol=iterations)))
names(LLR_results_anyrepression_noin69)=names(mod69left1$coefficients)
LLR_half_results_anyrepression_noin69=as.data.frame(t(matrix(unlist(LLR_half_anyrepression_noin69), ncol=iterations)))
names(LLR_half_results_anyrepression_noin69)=names(mod69left2$coefficients)
LLR_2_results_anyrepression_noin69=as.data.frame(t(matrix(unlist(LLR_2_anyrepression_noin69), ncol=iterations)))
names(LLR_2_results_anyrepression_noin69)=names(mod69left3$coefficients)
LLR_party_results_anyrepression_noin69=as.data.frame(t(matrix(unlist(LLR_party_anyrepression_noin69), ncol=iterations)))
names(LLR_party_results_anyrepression_noin69)=names(mod69left4$coefficients)
LLR_party_half_results_anyrepression_noin69=as.data.frame(t(matrix(unlist(LLR_party_half_anyrepression_noin69), ncol=iterations)))
names(LLR_party_half_results_anyrepression_noin69)=names(mod69left5$coefficients)
LLR_party_2_results_anyrepression_noin69=as.data.frame(t(matrix(unlist(LLR_party_2_anyrepression_noin69), ncol=iterations)))
names(LLR_party_2_results_anyrepression_noin69)=names(mod69left6$coefficients)

late_lower_anyrepression_noin69<-format(round(quantile(LLR_results_anyrepression_noin69$Tr, probs=.05), 2))
late_upper_anyrepression_noin69<-format(round(quantile(LLR_results_anyrepression_noin69$Tr, probs=.95), 2))
#write(late_lower_anyrepression_noin69, "late_lower_anyrepression_noin69.tex")
#write(late_upper_anyrepression_noin69, "late_upper_anyrepression_noin69.tex")

late_lower_anyrepression_noin69_95<-format(round(quantile(LLR_results_anyrepression_noin69$Tr, probs=.025), 2))
late_upper_anyrepression_noin69_95<-format(round(quantile(LLR_results_anyrepression_noin69$Tr, probs=.975), 2))
#write(late_lower_anyrepression_noin69_95, "late_lower_anyrepression_noin69_95.tex")
#write(late_upper_anyrepression_noin69_95, "late_upper_anyrepression_noin69_95.tex")

late_half_lower_anyrepression_noin69<-format(round(quantile(LLR_half_results_anyrepression_noin69$Tr, probs=.05), 2))
late_half_upper_anyrepression_noin69<-format(round(quantile(LLR_half_results_anyrepression_noin69$Tr, probs=.95), 2))
#write(late_half_lower_anyrepression_noin69, "late_half_lower_anyrepression_noin69.tex")
#write(late_half_upper_anyrepression_noin69, "late_half_upper_anyrepression_noin69.tex")

late_half_lower_anyrepression_noin69_95<-format(round(quantile(LLR_half_results_anyrepression_noin69$Tr, probs=.025), 2))
late_half_upper_anyrepression_noin69_95<-format(round(quantile(LLR_half_results_anyrepression_noin69$Tr, probs=.975), 2))
#write(late_half_lower_anyrepression_noin69_95, "late_half_lower_anyrepression_noin69_95.tex")
#write(late_half_upper_anyrepression_noin69_95, "late_half_upper_anyrepression_noin69_95.tex")

late_2_lower_anyrepression_noin69<-format(round(quantile(LLR_2_results_anyrepression_noin69$Tr, probs=.05), 2))
late_2_upper_anyrepression_noin69<-format(round(quantile(LLR_2_results_anyrepression_noin69$Tr, probs=.95), 2))
#write(late_2_lower_anyrepression_noin69, "late_2_lower_anyrepression_noin69.tex")
#write(late_2_upper_anyrepression_noin69, "late_2_upper_anyrepression_noin69.tex")

late_2_lower_anyrepression_noin69_95<-format(round(quantile(LLR_2_results_anyrepression_noin69$Tr, probs=.025), 2))
late_2_upper_anyrepression_noin69_95<-format(round(quantile(LLR_2_results_anyrepression_noin69$Tr, probs=.975), 2))
#write(late_2_lower_anyrepression_noin69_95, "late_2_lower_anyrepression_noin69_95.tex")
#write(late_2_upper_anyrepression_noin69_95, "late_2_upper_anyrepression_noin69_95.tex")

late_party_lower_anyrepression_noin69<-format(round(quantile(LLR_party_results_anyrepression_noin69$Tr, probs=.05), 2))
late_party_upper_anyrepression_noin69<-format(round(quantile(LLR_party_results_anyrepression_noin69$Tr, probs=.95), 2))
#write(late_party_lower_anyrepression_noin69, "late_party_lower_anyrepression_noin69.tex")
#write(late_party_upper_anyrepression_noin69, "late_party_upper_anyrepression_noin69.tex")

late_party_lower_anyrepression_noin69_95<-format(round(quantile(LLR_party_results_anyrepression_noin69$Tr, probs=.025), 2))
late_party_upper_anyrepression_noin69_95<-format(round(quantile(LLR_party_results_anyrepression_noin69$Tr, probs=.975), 2))
#write(late_party_lower_anyrepression_noin69_95, "late_party_lower_anyrepression_noin69_95.tex")
#write(late_party_upper_anyrepression_noin69_95, "late_party_upper_anyrepression_noin69_95.tex")

late_party_half_lower_anyrepression_noin69<-format(round(quantile(LLR_party_half_results_anyrepression_noin69$Tr, probs=.05), 2))
late_party_half_upper_anyrepression_noin69<-format(round(quantile(LLR_party_half_results_anyrepression_noin69$Tr, probs=.95), 2))
#write(late_party_half_lower_anyrepression_noin69, "late_party_half_lower_anyrepression_noin69.tex")
#write(late_party_half_upper_anyrepression_noin69, "late_party_half_upper_anyrepression_noin69.tex")

late_party_half_lower_anyrepression_noin69_95<-format(round(quantile(LLR_party_half_results_anyrepression_noin69$Tr, probs=.025), 2))
late_party_half_upper_anyrepression_noin69_95<-format(round(quantile(LLR_party_half_results_anyrepression_noin69$Tr, probs=.975), 2))
#write(late_party_half_lower_anyrepression_noin69_95, "late_party_half_lower_anyrepression_noin69_95.tex")
#write(late_party_half_upper_anyrepression_noin69_95, "late_party_half_upper_anyrepression_noin69_95.tex")

late_party_2_lower_anyrepression_noin69<-format(round(quantile(LLR_party_2_results_anyrepression_noin69$Tr, probs=.05), 2))
late_party_2_upper_anyrepression_noin69<-format(round(quantile(LLR_party_2_results_anyrepression_noin69$Tr, probs=.95), 2))
#write(late_party_2_lower_anyrepression_noin69, "late_party_2_lower_anyrepression_noin69.tex")
#write(late_party_2_upper_anyrepression_noin69, "late_party_2_upper_anyrepression_noin69.tex")

late_party_2_lower_anyrepression_noin69_95<-format(round(quantile(LLR_party_2_results_anyrepression_noin69$Tr, probs=.025), 2))
late_party_2_upper_anyrepression_noin69_95<-format(round(quantile(LLR_party_2_results_anyrepression_noin69$Tr, probs=.975), 2))
#write(late_party_2_lower_anyrepression_noin69_95, "late_party_2_lower_anyrepression_noin69_95.tex")
#write(late_party_2_upper_anyrepression_noin69_95, "late_party_2_upper_anyrepression_noin69_95.tex")




##LLR 69
rdd_all_nounelected69<-RDestimate(victim~quotient_diff, data=dips69_nounelected, cluster=dips69_nounelected$Distrito)
rdd_all_party_nounelected69<-RDestimate(victim~quotient_diff|Partido, data=dips69_nounelected, cluster=dips69_nounelected$Distrito)

#write(format(round(rdd_all_nounelected69$est[1], 2)), "late_nounelected69.tex")
#write(format(round(rdd_all_nounelected69$est[2], 2)), "half_bw_nounelected69.tex")
#write(format(round(rdd_all_nounelected69$est[3], 2)), "double_bw_nounelected69.tex")

#write(format(round(rdd_all_party_nounelected69$est[1], 2)), "latep_nounelected69.tex")
#write(format(round(rdd_all_party_nounelected69$est[2], 2)), "halfp_nounelected69.tex")
#write(format(round(rdd_all_party_nounelected69$est[3], 2)), "doublep_nounelected69.tex")

#write(rdd_all_nounelected69$obs[1], "obs_late_nounelected69.tex")
#write(rdd_all_nounelected69$obs[2], "obs_half_late_nounelected69.tex")
#write(rdd_all_nounelected69$obs[3], "obs_double_late_nounelected69.tex")

#write(format(round(rdd_all_nounelected69$bw[1],2)), "bw_late_nounelected69.tex")
#write(format(round(rdd_all_nounelected69$bw[2],2)), "bw_half_late_nounelected69.tex")
#write(format(round(rdd_all_nounelected69$bw[3],2)), "bw_double_late_nounelected69.tex")

dips69_nounelected$distritos_num<-as.numeric(dips69_nounelected$Distrito)

X <-dips69_nounelected$quotient_diff
Xl <- (X < 0) * X
Xr <- (X >= 0) * X
Tr <- as.integer(X >= 0)
Y <- dips69_nounelected$victim
cluster<-dips69_nounelected$distritos_num
bw=IKbandwidth(X = X, Y = Y, cutpoint = 0, kernel = "triangular")
w<-kernelwts(dips69_nounelected$quotient_diff, 0, bw=bw, kernel="triangular")
whalf<-kernelwts(dips69_nounelected$quotient_diff, 0, bw=bw/2, kernel="triangular")
w2<-kernelwts(dips69_nounelected$quotient_diff, 0, bw=2*bw, kernel="triangular")
partydummies<-dummy(dips69_nounelected$Partido)
head(partydummies)
colnames(partydummies)<-c("PC", "PDC", "PN", "PR", "PS")
partydummies<-data.frame(partydummies)

PDC<-partydummies$PDC
PN<-partydummies$PN
PR<-partydummies$PR
PS<-partydummies$PS

cluster <- dips69_nounelected$distritos_num
data<-data.frame(Y, Tr, X, Xl, Xr, w, whalf, w2, cluster, PDC, PN, PR, PS)

distritos_num<-unique(cluster)
n=length(distritos_num)
LLR_nounelected69=list()
LLR_half_nounelected69=list()
LLR_2_nounelected69=list()
LLR_party_nounelected69=list()
LLR_party_half_nounelected69=list()
LLR_party_2_nounelected69=list()
for (j in 1:iterations){
  v=sample(distritos_num, n, replace=TRUE)
  newdata=NULL
  
  for (i in 1:n){
    newdata=rbind(newdata, subset(data, data$cluster==v[i]))
  }
  mod691<-lm(Y ~ Tr + Xl + Xr, weights = w, data = subset(newdata, w > 0))
  mod692<-lm(Y ~ Tr + Xl + Xr, weights=whalf, data=subset(newdata, whalf>0))
  mod693<-lm(Y ~ Tr + Xl + Xr, weights=w2, data=subset(newdata, w2>0))
  
  mod694<-lm(Y ~ Tr + Xl + Xr+PDC+PN+PR+PS, weights = w, data = subset(newdata, w > 0))
  mod695<-lm(Y ~ Tr + Xl + Xr+PDC+PN+PR+PS, weights = whalf, data = subset(newdata, whalf > 0))
  mod696<-lm(Y ~ Tr + Xl + Xr+PDC+PN+PR+PS, weights = w2, data = subset(newdata, w2 > 0))
  
  LLR_nounelected69[[j]]=coefficients(mod691)
  LLR_half_nounelected69[[j]]=coefficients(mod692)
  LLR_2_nounelected69[[j]]=coefficients(mod693)
  LLR_party_nounelected69[[j]]=coefficients(mod694)
  LLR_party_half_nounelected69[[j]]=coefficients(mod695)
  LLR_party_2_nounelected69[[j]]=coefficients(mod696)
}
LLR_results_nounelected69=as.data.frame(t(matrix(unlist(LLR_nounelected69), ncol=iterations)))
names(LLR_results_nounelected69)=names(mod691$coefficients)
LLR_half_results_nounelected69=as.data.frame(t(matrix(unlist(LLR_half_nounelected69), ncol=iterations)))
names(LLR_half_results_nounelected69)=names(mod692$coefficients)
LLR_2_results_nounelected69=as.data.frame(t(matrix(unlist(LLR_2_nounelected69), ncol=iterations)))
names(LLR_2_results_nounelected69)=names(mod693$coefficients)
LLR_party_results_nounelected69=as.data.frame(t(matrix(unlist(LLR_party_nounelected69), ncol=iterations)))
names(LLR_party_results_nounelected69)=names(mod694$coefficients)
LLR_party_half_results_nounelected69=as.data.frame(t(matrix(unlist(LLR_party_half_nounelected69), ncol=iterations)))
names(LLR_party_half_results_nounelected69)=names(mod694$coefficients)
LLR_party_2_results_nounelected69=as.data.frame(t(matrix(unlist(LLR_party_2_nounelected69), ncol=iterations)))
names(LLR_party_2_results_nounelected69)=names(mod695$coefficients)

late_lower_nounelected69<-format(round(quantile(LLR_results_nounelected69$Tr, probs=.05), 2))
late_upper_nounelected69<-format(round(quantile(LLR_results_nounelected69$Tr, probs=.95), 2))
#write(late_lower_nounelected69, "late_lower_nounelected69.tex")
#write(late_upper_nounelected69, "late_upper_nounelected69.tex")

late_half_lower_nounelected69<-format(round(quantile(LLR_half_results_nounelected69$Tr, probs=.05), 2))
late_half_upper_nounelected69<-format(round(quantile(LLR_half_results_nounelected69$Tr, probs=.95), 2))
#write(late_half_lower_nounelected69, "late_half_lower_nounelected69.tex")
#write(late_half_upper_nounelected69, "late_half_upper_nounelected69.tex")

late_2_lower_nounelected69<-format(round(quantile(LLR_2_results_nounelected69$Tr, probs=.05), 2))
late_2_upper_nounelected69<-format(round(quantile(LLR_2_results_nounelected69$Tr, probs=.95), 2))
#write(late_2_lower_nounelected69, "late_2_lower_nounelected69.tex")
#write(late_2_upper_nounelected69, "late_2_upper_nounelected69.tex")

late_party_lower_nounelected69<-format(round(quantile(LLR_party_results_nounelected69$Tr, probs=.05), 2))
late_party_upper_nounelected69<-format(round(quantile(LLR_party_results_nounelected69$Tr, probs=.95), 2))
#write(late_party_lower_nounelected69, "late_party_lower_nounelected69.tex")
#write(late_party_upper_nounelected69, "late_party_upper_nounelected69.tex")

late_party_half_lower_nounelected69<-format(round(quantile(LLR_party_half_results_nounelected69$Tr, probs=.05), 2))
late_party_half_upper_nounelected69<-format(round(quantile(LLR_party_half_results_nounelected69$Tr, probs=.95), 2))
#write(late_party_half_lower_nounelected69, "late_party_half_lower_nounelected69.tex")
#write(late_party_half_upper_nounelected69, "late_party_half_upper_nounelected69.tex")

late_party_2_lower_nounelected69<-format(round(quantile(LLR_party_2_results_nounelected69$Tr, probs=.05), 2))
late_party_2_upper_nounelected69<-format(round(quantile(LLR_party_2_results_nounelected69$Tr, probs=.95), 2))
#write(late_party_2_lower_nounelected69, "late_party_2_lower_nounelected69.tex")
#write(late_party_2_upper_nounelected69, "late_party_2_upper_nounelected69.tex")

##anyrepression
rdd_all_anyrepression_nounelected69<-RDestimate(anyrepression~quotient_diff, data=dips69_nounelected, cluster=dips69_nounelected$Distrito)
rdd_all_party_anyrepression_nounelected69<-RDestimate(anyrepression~quotient_diff|Partido, data=dips69_nounelected, cluster=dips69_nounelected$Distrito, bw=2)

#write(format(round(rdd_all_anyrepression_nounelected69$est[1], 2)), "late_anyrepression_nounelected69.tex")
#write(format(round(rdd_all_anyrepression_nounelected69$est[2], 2)), "half_bw_anyrepression_nounelected69.tex")
#write(format(round(rdd_all_anyrepression_nounelected69$est[3], 2)), "double_bw_anyrepression_nounelected69.tex")

#write(format(round(rdd_all_party_anyrepression_nounelected69$est[1], 2)), "latep_anyrepression_nounelected69.tex")
#write(format(round(rdd_all_party_anyrepression_nounelected69$est[2], 2)), "halfp_anyrepression_nounelected69.tex")
#write(format(round(rdd_all_party_anyrepression_nounelected69$est[3], 2)), "doublep_anyrepression_nounelected69.tex")

#write(rdd_all_anyrepression_nounelected69$obs[1], "obs_late_anyrepression_nounelected69.tex")
#write(rdd_all_anyrepression_nounelected69$obs[2], "obs_half_late_anyrepression_nounelected69.tex")
#write(rdd_all_anyrepression_nounelected69$obs[3], "obs_double_late_anyrepression_nounelected69.tex")

#write(format(round(rdd_all_anyrepression_nounelected69$bw[1],2)), "bw_late_anyrepression_nounelected69.tex")
#write(format(round(rdd_all_anyrepression_nounelected69$bw[2],2)), "bw_half_late_anyrepression_nounelected69.tex")
#write(format(round(rdd_all_anyrepression_nounelected69$bw[3],2)), "bw_double_late_anyrepression_nounelected69.tex")

dips69_nounelected$distritos_num<-as.numeric(dips69_nounelected$Distrito)

X <-dips69_nounelected$quotient_diff
Xl <- (X < 0) * X
Xr <- (X >= 0) * X
Tr <- as.integer(X >= 0)
Y <- dips69_nounelected$anyrepression
cluster<-dips69_nounelected$distritos_num
bw=IKbandwidth(X = X, Y = Y, cutpoint = 0, kernel = "triangular")
w<-kernelwts(dips69_nounelected$quotient_diff, 0, bw=bw, kernel="triangular")
whalf<-kernelwts(dips69_nounelected$quotient_diff, 0, bw=bw/2, kernel="triangular")
w2<-kernelwts(dips69_nounelected$quotient_diff, 0, bw=2*bw, kernel="triangular")

cluster <- dips69_nounelected$distritos_num
data<-data.frame(Y, Tr, X, Xl, Xr, w, whalf, w2, cluster, PDC, PN, PR, PS)

distritos_num<-unique(cluster)
n=length(distritos_num)
LLR_anyrepression_nounelected69=list()
LLR_half_anyrepression_nounelected69=list()
LLR_2_anyrepression_nounelected69=list()
LLR_party_anyrepression_nounelected69=list()
LLR_party_half_anyrepression_nounelected69=list()
LLR_party_2_anyrepression_nounelected69=list()
for (j in 1:iterations){
  v=sample(distritos_num, n, replace=TRUE)
  newdata=NULL
  
  for (i in 1:n){
    newdata=rbind(newdata, subset(data, data$cluster==v[i]))
  }
  mod69left1<-lm(Y ~ Tr + Xl + Xr, weights = w, data = subset(newdata, w > 0))
  mod69left2<-lm(Y ~ Tr + Xl + Xr, weights=whalf, data=subset(newdata, whalf>0))
  mod69left3<-lm(Y ~ Tr + Xl + Xr, weights=w2, data=subset(newdata, w2>0))
  
  mod69left4<-lm(Y ~ Tr + Xl + Xr+PDC+PN+PR+PS, weights = w, data = subset(newdata, w > 0))
  mod69left5<-lm(Y ~ Tr + Xl + Xr+PDC+PN+PR+PS, weights = whalf, data = subset(newdata, whalf > 0))
  mod69left6<-lm(Y ~ Tr + Xl + Xr+PDC+PN+PR+PS, weights = w2, data = subset(newdata, w2 > 0))
  
  LLR_anyrepression_nounelected69[[j]]=coefficients(mod69left1)
  LLR_half_anyrepression_nounelected69[[j]]=coefficients(mod69left2)
  LLR_2_anyrepression_nounelected69[[j]]=coefficients(mod69left3)
  LLR_party_anyrepression_nounelected69[[j]]=coefficients(mod69left4)
  LLR_party_half_anyrepression_nounelected69[[j]]=coefficients(mod69left5)
  LLR_party_2_anyrepression_nounelected69[[j]]=coefficients(mod69left6)
}
LLR_results_anyrepression_nounelected69=as.data.frame(t(matrix(unlist(LLR_anyrepression_nounelected69), ncol=iterations)))
names(LLR_results_anyrepression_nounelected69)=names(mod69left1$coefficients)
LLR_half_results_anyrepression_nounelected69=as.data.frame(t(matrix(unlist(LLR_half_anyrepression_nounelected69), ncol=iterations)))
names(LLR_half_results_anyrepression_nounelected69)=names(mod69left2$coefficients)
LLR_2_results_anyrepression_nounelected69=as.data.frame(t(matrix(unlist(LLR_2_anyrepression_nounelected69), ncol=iterations)))
names(LLR_2_results_anyrepression_nounelected69)=names(mod69left3$coefficients)
LLR_party_results_anyrepression_nounelected69=as.data.frame(t(matrix(unlist(LLR_party_anyrepression_nounelected69), ncol=iterations)))
names(LLR_party_results_anyrepression_nounelected69)=names(mod69left4$coefficients)
LLR_party_half_results_anyrepression_nounelected69=as.data.frame(t(matrix(unlist(LLR_party_half_anyrepression_nounelected69), ncol=iterations)))
names(LLR_party_half_results_anyrepression_nounelected69)=names(mod69left5$coefficients)
LLR_party_2_results_anyrepression_nounelected69=as.data.frame(t(matrix(unlist(LLR_party_2_anyrepression_nounelected69), ncol=iterations)))
names(LLR_party_2_results_anyrepression_nounelected69)=names(mod69left6$coefficients)

late_lower_anyrepression_nounelected69<-format(round(quantile(LLR_results_anyrepression_nounelected69$Tr, probs=.05), 2))
late_upper_anyrepression_nounelected69<-format(round(quantile(LLR_results_anyrepression_nounelected69$Tr, probs=.95), 2))
#write(late_lower_anyrepression_nounelected69, "late_lower_anyrepression_nounelected69.tex")
#write(late_upper_anyrepression_nounelected69, "late_upper_anyrepression_nounelected69.tex")

late_half_lower_anyrepression_nounelected69<-format(round(quantile(LLR_half_results_anyrepression_nounelected69$Tr, probs=.05), 2))
late_half_upper_anyrepression_nounelected69<-format(round(quantile(LLR_half_results_anyrepression_nounelected69$Tr, probs=.95), 2))
#write(late_half_lower_anyrepression_nounelected69, "late_half_lower_anyrepression_nounelected69.tex")
#write(late_half_upper_anyrepression_nounelected69, "late_half_upper_anyrepression_nounelected69.tex")
late_2_lower_anyrepression_nounelected69<-format(round(quantile(LLR_2_results_anyrepression_nounelected69$Tr, probs=.05), 2))
late_2_upper_anyrepression_nounelected69<-format(round(quantile(LLR_2_results_anyrepression_nounelected69$Tr, probs=.95), 2))
#write(late_2_lower_anyrepression_nounelected69, "late_2_lower_anyrepression_nounelected69.tex")
#write(late_2_upper_anyrepression_nounelected69, "late_2_upper_anyrepression_nounelected69.tex")

late_party_lower_anyrepression_nounelected69<-format(round(quantile(LLR_party_results_anyrepression_nounelected69$Tr, probs=.05), 2))
late_party_upper_anyrepression_nounelected69<-format(round(quantile(LLR_party_results_anyrepression_nounelected69$Tr, probs=.95), 2))
#write(late_party_lower_anyrepression_nounelected69, "late_party_lower_anyrepression_nounelected69.tex")
#write(late_party_upper_anyrepression_nounelected69, "late_party_upper_anyrepression_nounelected69.tex")

late_party_half_lower_anyrepression_nounelected69<-format(round(quantile(LLR_party_half_results_anyrepression_nounelected69$Tr, probs=.05), 2))
late_party_half_upper_anyrepression_nounelected69<-format(round(quantile(LLR_party_half_results_anyrepression_nounelected69$Tr, probs=.95), 2))
#write(late_party_half_lower_anyrepression_nounelected69, "late_party_half_lower_anyrepression_nounelected69.tex")
#write(late_party_half_upper_anyrepression_nounelected69, "late_party_half_upper_anyrepression_nounelected69.tex")

late_party_2_lower_anyrepression_nounelected69<-format(round(quantile(LLR_party_2_results_anyrepression_nounelected69$Tr, probs=.05), 2))
late_party_2_upper_anyrepression_nounelected69<-format(round(quantile(LLR_party_2_results_anyrepression_nounelected69$Tr, probs=.95), 2))

#write(late_party_2_lower_anyrepression_nounelected69, "late_party_2_lower_anyrepression_nounelected69.tex")
#write(late_party_2_upper_anyrepression_nounelected69, "late_party_2_upper_anyrepression_nounelected69.tex")




###########################################################
#################Table C8: 1971 Candidates T-tests#########
###########################################################
ttest_anyrepression_regidores<-ttest_electedvunelected(regidores$anyrepression, regidores)
##write(format(round(as.numeric(as.character(ttest_anyrepression_regidores$elected[1])), 2)), "mean_anyrepression_regidores_elected.tex")
##write(format(round(as.numeric(as.character(ttest_anyrepression_regidores$unelected[1])), 2)), "mean_anyrepression_regidores_unelected.tex")
##write(format(round(as.numeric(as.character(ttest_anyrepression_regidores$difference[1])), 2)), "mean_anyrepression_regidores_difference.tex")

ttest_physicalcoercion_regidores<-ttest_electedvunelected(regidores$victim, regidores)
##write(format(round(as.numeric(as.character(ttest_physicalcoercion_regidores$elected[1])), 2)), "mean_physicalcoercion_regidores_elected.tex")
##write(format(round(as.numeric(as.character(ttest_physicalcoercion_regidores$unelected[1])), 2)), "mean_physicalcoercion_regidores_unelected.tex")
##write(format(round(as.numeric(as.character(ttest_physicalcoercion_regidores$difference[1])), 2)), "mean_physicalcoercion_regidores_difference.tex")




#######################################################
###################Table C9: Local, close##############
#######################################################

n=6
regidores_full$close<-ifelse(regidores_full$vote_perc<n, 1, 0)
communist<-subset(regidores_full, PARTIDO=="COMUNISTA")
socialist<-subset(regidores_full, PARTIDO=="SOCIALISTA")
communist$close<-ifelse(communist$vote_perc<n, 1, 0)
socialist$close<-ifelse(socialist$vote_perc<n, 1, 0)

keep<-unique(regidores_full$COMUNA[regidores_full$close==1])
keep_com<-unique(communist$COMUNA[communist$close==1])
keep_soc<-unique(socialist$COMUNA[socialist$close==1])

close_regidores<-regidores_full[regidores_full$COMUNA %in% keep, ]
close_regidores_com<-communist[communist$COMUNA %in% keep_com, ]
close_regidores_soc<-socialist[socialist$COMUNA %in% keep_soc, ]

close_regidores$vote_perc[is.na(close_regidores$vote_perc)] <- 0
close_regidores_com $vote_perc[is.na(close_regidores_com $vote_perc)] <- 0
close_regidores_soc $vote_perc[is.na(close_regidores_soc $vote_perc)] <- 0

close_regidores_subset<-subset(close_regidores, close_regidores$vote_perc<n)
close_regidores_com_subset<-subset(close_regidores_com, close_regidores_com$vote_perc<n)
close_regidores_soc_subset<-subset(close_regidores_soc, close_regidores_soc$vote_perc<n)

ttest_regidores_anyrepression_close<-ttest_electedvunelected(close_regidores_subset$anyrepression, close_regidores_subset)

##write(format(round(as.numeric(as.character(ttest_regidores_anyrepression_close$elected[1])), 2)), "mean_anyrepression_regidores_close_elected.tex")
##write(format(round(as.numeric(as.character(ttest_regidores_anyrepression_close$unelected[1])), 2)), "mean_anyrepression_regidores_close_unelected.tex")
##write(format(round(as.numeric(as.character(ttest_regidores_anyrepression_close$difference[1])), 2)), "mean_anyrepression_regidores_close_difference.tex")

ttest_regidores_physicalcoercion_close<-ttest_electedvunelected(close_regidores_subset$victim, close_regidores_subset)

##write(format(round(as.numeric(as.character(ttest_regidores_physicalcoercion_close$elected[1])), 2)), "mean_physicalcoercion_regidores_close_elected.tex")
##write(format(round(as.numeric(as.character(ttest_regidores_physicalcoercion_close$unelected[1])), 2)), "mean_physicalcoercion_regidores_close_unelected.tex")
##write(format(round(as.numeric(as.character(ttest_regidores_physicalcoercion_close$difference[1])), 2)), "mean_physicalcoercion_regidores_close_difference.tex")



